473,779 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A simple class?

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::ofstream out;
out.open(logfil e, ios_base::app);
//out << "************** *************** *************** **************\ n";
out.close();
*/
}

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

void logger::trace(c har* msg) {
}

void logger::debug(c har* msg) {
}

void logger::info(ch ar* msg) {
}

void logger::warn(ch ar* msg) {
}

void logger::fatal(c har* msg) {
}

}
Jul 22 '05 #1
4 1956
David wrote:
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 "instantiat e 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::ofstream out;
out.open(logfil e, ios_base::app);
//out << "************** *************** *************** **************\ n";
out.close();
*/
Tis constructor does nothing, AFAICS.
}

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

void logger::trace(c har* msg) {
}

void logger::debug(c har* msg) {
}

void logger::info(ch ar* msg) {
}

void logger::warn(ch ar* msg) {
}

void logger::fatal(c har* 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
Jul 22 '05 #2
David wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3
Karl Heinz Buchegger <kb******@gasca d.at> wrote in message news:<41******* ********@gascad .at>...
David wrote:

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.


Sorry I forgot the line where I instantiate the Logger:

Logger log("TermCache. log");
I changed this to
Logger *log = new Logger("TermCac he.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(s izeof(getenv("H OME")) + 1 +
sizeof(file));
//free(logfile);
strcpy(logfile, getenv("HOME")) ;
strcat(logfile, "/");
strcat(logfile, file);
cout << "Logging to '" << logfile << "'\n";
std::ofstream out;
out.open(logfil e, ios_base::in | ios_base::out | ios_base::app);
char* logmsg = "************** *************** *************** ***********\n";
out << logmsg;
out.write(logms g, sizeof(logmsg)) ;
out.close();
}

void setLevel(int logLevel) {
level = logLevel;
}
...
};
Jul 22 '05 #4
David wrote:
Karl Heinz Buchegger <kb******@gasca d.at> wrote in message news:<41******* ********@gascad .at>...
David wrote:
Hello,
I want to write a simple logging class. But when I instantiate the
logger with
Logger::logg er 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.

Sorry I forgot the line where I instantiate the Logger:

Logger log("TermCache. log");
I changed this to
Logger *log = new Logger("TermCac he.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(s izeof(getenv("H OME")) + 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::ofstream out;
out.open(logfil e, ios_base::in | ios_base::out | ios_base::app);
Why ios_base :: in ?

char* logmsg = "************** *************** *************** ***********\n";
out << logmsg;
out.write(logms g, 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 *).

--
Karthik. http://akktech.blogspot.com .
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3699
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
1
9492
by: Derrick | last post by:
I have the below simple class - public class MyClass { protected string m_stName; protected string m_stCode; protected int m_iId; public MyClass() {}
3
1335
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
8
1386
by: John | last post by:
Hi Could someone give me pointers on how to change this code (belwo) into a simple class? Thanks Regards Imports System
17
19310
by: RSH | last post by:
I am really trying to grasp the concept of OOP as it applies to C#. I am looking at trying to set up a simple Employee Class but I am having trouble conceptualizing what this class should look like. I was hoping someone might be able to simply outline what I envision my class to look like: Basically I am envisioning a Class called Employee: I imagine it would have many properties(?) such as:
9
2667
by: Andrew | last post by:
Hi, I implemented a simple WMI Provider in C#. It is a service which expose 10 instances of a simple WMI Class. The WMI class pnly expose 4 public properties (Value,Min,Max,StdValue) which only return some constant data. First, I was using MOM 2005 (Windows 2003) to interogate the instances values.
8
1728
by: Bern McCarty | last post by:
I have a simple ref class in its own namespace that needs to coexist with a legacy typedef alias for "unsigned int" in the global namespace that has the identifier as itself. Everything compiles fine with the old MEC++ syntax, but I cannot figure out how to write the code so that it will compile in C++/CLI. Can someone tell me how? Here is the code in both syntaxes: // This MEC++ code compiles just fine with VC8 using cl -c...
14
2987
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
10
2137
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
17
5819
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.