A simple class?

D

David

Hello,
I want to write a simple logging class. But when I instantiate the
logger with
Logger::logger log("log.log");
I get a "Bus Error - core dumped". Can anybody help?

Thanks beforehand,
David

This is my logger.hpp file (I tried it with .h, too.)

#define TRACE 0
#define DEBUG 1
#define INFO 2
#define WARN 3
#define FATAL 4

namespace Logger {

class logger {
public:
logger(char* logfile);
void setLevel(int level);
void trace(char* msg);
void debug(char* msg);
void info(char* msg);
void warn(char* msg);
void fatal(char* msg);
};

}


This is my logger.cc File

#include "logger.hpp"
#include <fstream>
//using namespace std;

namespace Logger {

//char* logfile;
//int level = INFO;

logger::logger(char* file) {
/*
logfile = file;
std::eek:fstream out;
out.open(logfile, ios_base::app);
//out << "**********************************************************\n";
out.close();
*/
}

void logger::setLevel(int logLevel) {
//level = logLevel;
}

void logger::trace(char* msg) {
}

void logger::debug(char* msg) {
}

void logger::info(char* msg) {
}

void logger::warn(char* msg) {
}

void logger::fatal(char* msg) {
}

}
 
V

Victor Bazarov

David said:
Hello,
I want to write a simple logging class. But when I instantiate the
logger with
Logger::logger log("log.log");
I get a "Bus Error - core dumped". Can anybody help?

Yes, as soon as you post the real code. Especially the part where
you "instantiate the logger".
Thanks beforehand,
David

This is my logger.hpp file (I tried it with .h, too.)

#define TRACE 0
#define DEBUG 1
#define INFO 2
#define WARN 3
#define FATAL 4

namespace Logger {

class logger {
public:
logger(char* logfile);
void setLevel(int level);
void trace(char* msg);
void debug(char* msg);
void info(char* msg);
void warn(char* msg);
void fatal(char* msg);
};

Your class doesn't appear to keep anything. How is it going to log
anything anywhere?
}


This is my logger.cc File

#include "logger.hpp"
#include <fstream>
//using namespace std;

namespace Logger {

//char* logfile;
//int level = INFO;

logger::logger(char* file) {
/*
logfile = file;
std::eek:fstream out;
out.open(logfile, ios_base::app);
//out << "**********************************************************\n";
out.close();
*/

Tis constructor does nothing, AFAICS.
}

void logger::setLevel(int logLevel) {
//level = logLevel;
}

void logger::trace(char* msg) {
}

void logger::debug(char* msg) {
}

void logger::info(char* msg) {
}

void logger::warn(char* msg) {
}

void logger::fatal(char* msg) {
}

}

Again, everything is commented out or hasn't been implemented yet.
How can that produce any errors? I am puzzled probably no less than
you.

V
 
K

Karl Heinz Buchegger

David said:
Hello,
I want to write a simple logging class. But when I instantiate the
logger with
Logger::logger log("log.log");
I get a "Bus Error - core dumped". Can anybody help?

Is the class you posted exactly what you compiled?
I ask because in the posted code all functionality in
the class members is commented away, leaving nothing that
could crash.

Please try a minimal, complete program (that includes main()! )
which shows the same problem. Make sure that this program indeed
crashes on your system and then post it.
 
D

David

Karl Heinz Buchegger said:
Is the class you posted exactly what you compiled?
I ask because in the posted code all functionality in
the class members is commented away, leaving nothing that
could crash.

Please try a minimal, complete program (that includes main()! )
which shows the same problem. Make sure that this program indeed
crashes on your system and then post it.

Sorry I forgot the line where I instantiate the Logger:

Logger log("TermCache.log");
I changed this to
Logger *log = new Logger("TermCache.log");

The logger does nothing because
I wanted to instantiate it correctly first. That the empty constructor
(to me) seemed to cause the error, stemmed from the fact that I
expected the makefile to
compile the class when it changed, which it doesn't, sorry.

But I managed to find the problem. I assigned the value of a char* to
another
char* for which I didn't allocate memory yet. Now I corrected this and
it works.

This is my new logger now. I now have a new problem. It won't write to
the file
'/home/kensche/TermCache.log' which is the value of 'logfile'.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

#define TRACE 0
#define DEBUG 1
#define INFO 2
#define WARN 3
#define FATAL 4

class Logger {

char* logfile;
int level;

public:
Logger(const char* file) {
//cout << "Logging to '" << getenv("HOME") << "/" << file <<
"'.\n";
level = INFO;
logfile = (char*)malloc(sizeof(getenv("HOME")) + 1 +
sizeof(file));
//free(logfile);
strcpy(logfile, getenv("HOME"));
strcat(logfile, "/");
strcat(logfile, file);
cout << "Logging to '" << logfile << "'\n";
std::eek:fstream out;
out.open(logfile, ios_base::in | ios_base::eek:ut | ios_base::app);
char* logmsg = "*******************************************************\n";
out << logmsg;
out.write(logmsg, sizeof(logmsg));
out.close();
}

void setLevel(int logLevel) {
level = logLevel;
}
...
};
 
K

Karthik Kumar

David said:
Sorry I forgot the line where I instantiate the Logger:

Logger log("TermCache.log");
I changed this to
Logger *log = new Logger("TermCache.log");

The logger does nothing because
I wanted to instantiate it correctly first. That the empty constructor
(to me) seemed to cause the error, stemmed from the fact that I
expected the makefile to
compile the class when it changed, which it doesn't, sorry.

But I managed to find the problem. I assigned the value of a char* to
another
char* for which I didn't allocate memory yet. Now I corrected this and
it works.

This is my new logger now. I now have a new problem. It won't write to
the file
'/home/kensche/TermCache.log' which is the value of 'logfile'.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

#define TRACE 0
#define DEBUG 1
#define INFO 2
#define WARN 3
#define FATAL 4

class Logger {

char* logfile;
int level;

public:
Logger(const char* file) {
//cout << "Logging to '" << getenv("HOME") << "/" << file <<
"'.\n";
level = INFO;
logfile = (char*)malloc(sizeof(getenv("HOME")) + 1 +

This cast is not necessary for malloc.
sizeof(file));
//free(logfile);
strcpy(logfile, getenv("HOME"));
strcat(logfile, "/");
strcat(logfile, file);

You seem to be converting C code to C++ code.
Take a look at stringstreams.
cout << "Logging to '" << logfile << "'\n";
std::eek:fstream out;
out.open(logfile, ios_base::in | ios_base::eek:ut | ios_base::app);

Why ios_base :: in ?

char* logmsg = "*******************************************************\n";
out << logmsg;
out.write(logmsg, sizeof(logmsg));

The culprit is here *sizeof(logmsg)*
This is going to return the size of a pointer variable , specific to an
implementation. May not be what you want. May be ,
you want strlen(const char *).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top