P
Prashant
Hi, i'm having an issue with combining functions from C and C++. I'm
trying to create a logging function that displays the file, function
and line number of the code that wants to log a certain message. The
function is called MainDisplay and outputs to a file called mylog.log.
For some reason, I SOMETIMES get a segmentation fault when I try and
open an output stream to the file. The code looks something like
this:
//file display.h
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <fstream.h>
using namespace std;
#define MainDisplay(msg, ...) Show(__FILE__,\
__PRETTY_FUNCTION__, \
__LINE__, msg, ## __VA_ARGS___);
void Show(const char* file,
const char* fcn,
int line,
char* display, ...) {
va_list va;
va_start(va, display);
char display2[256];
vsprintf(display2, display, va);
va_end(va);
char linestr[10];
sprintf(linestr, "%d", line);
string s = file;
s += ": function ";
s += fcn;
s += ": line ";
s += linestr;
s += ": ";
s += display2;
log_stream.open("my_log.log", ios::app);
log_stream << s;
log_stream.close();
}
//end display.h
The above would be called from any file as follows:
//file randomfile.cpp
#include "display.h"
void randomfunction() {
char* name = "bob";
MainDisplay("Hi, my name is %s\n", name);
}
//end randomfile.cpp
But, sometimes, and not always, I get a segmentation fault when I try
and do
log_stream.open("my_log.log", ios::app);
From debugging i've found that this happens if i'm calling MainDisplay
from a function that does a lot of C-style string manipulation.
Whats going on here?
trying to create a logging function that displays the file, function
and line number of the code that wants to log a certain message. The
function is called MainDisplay and outputs to a file called mylog.log.
For some reason, I SOMETIMES get a segmentation fault when I try and
open an output stream to the file. The code looks something like
this:
//file display.h
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <fstream.h>
using namespace std;
#define MainDisplay(msg, ...) Show(__FILE__,\
__PRETTY_FUNCTION__, \
__LINE__, msg, ## __VA_ARGS___);
void Show(const char* file,
const char* fcn,
int line,
char* display, ...) {
va_list va;
va_start(va, display);
char display2[256];
vsprintf(display2, display, va);
va_end(va);
char linestr[10];
sprintf(linestr, "%d", line);
string s = file;
s += ": function ";
s += fcn;
s += ": line ";
s += linestr;
s += ": ";
s += display2;
log_stream.open("my_log.log", ios::app);
log_stream << s;
log_stream.close();
}
//end display.h
The above would be called from any file as follows:
//file randomfile.cpp
#include "display.h"
void randomfunction() {
char* name = "bob";
MainDisplay("Hi, my name is %s\n", name);
}
//end randomfile.cpp
But, sometimes, and not always, I get a segmentation fault when I try
and do
log_stream.open("my_log.log", ios::app);
From debugging i've found that this happens if i'm calling MainDisplay
from a function that does a lot of C-style string manipulation.
Whats going on here?