2

I am trying to code a simple log file writer that will be used in many .cpp files in one project. So far this is the code:

enter code here
#include"log.h"
logger::logger()
{
    m_outStream.open("LogFile");
    m_outStream<<"********** LOG ENTRY STARTED **********"<<endl;
}

logger::~logger()
{
   m_outStream.close();
}

void logger::logEntry(const char* filename, const unsigned long lineno,  const char* str) 
{
    m_outStream<<"--> NAME: "<<filename<<" | LINE NUMBER : "<<lineno<<" | "<<str<<endl;
} 

Main can be something like

main()
{
    func1();
    func2();
    .
    .
    .
}

The functions called from main may make other function calls in their bodies. Now, how or where do i declare a logObject to make sure inside every function, i will have a logObject available that will facilitate my call to logEntry().

I thought making it public somewhere, and i think it makes sense for my case, but i am not sure if that is a good practice. Should the objects that work on log files be public? Any insights?

Sahil
  • 359
  • 2
  • 5
  • 13
  • 1
    I'm currently having the same issue ... my approach is to declare the Logger class static, so i can call Log::logEntry(...) everywhere without managing any instances – kylecorver Apr 22 '15 at 07:52
  • @kylecorver, there are quite a few differences between using static class and a singleton, though - see here: http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – Plamen G Apr 22 '15 at 08:10
  • Yes I did just read that yesterday ... but I think the static class is more useful. Or are there any disadvantages i will run into? – kylecorver Apr 22 '15 at 08:16

1 Answers1

2

I'd suggest using the Singleton pattern to implement this. It is used very often for global functionality like logging that is needed throughout the application.

It does not make sense to create a new instance every time you need to log something, so that's why the Singleton pattern is a really good fit. It will either create a single instance the first time you need logging or return one if it's already been instantiated.

Once you define it, you can include it everywhere you need to use it (see the second sample for how to include it if you're having trouble with this).

See the first example here or this sample implementation if you like it better, also refer to this existing thread.

Good luck! Let me know if you need more information.

Community
  • 1
  • 1
Plamen G
  • 4,729
  • 4
  • 33
  • 44
  • Thanks, it seems a good fit. But what do you think if i declare logger object to be extern in log.h and allocate memory for it in log.cpp I just did that and it works. However, i am not sure which way will be better. Singleton or Extern? – Sahil Apr 22 '15 at 07:12
  • Glad you found my answer helpful! If I understand you correctly this way you'll allocate memory for the log object in each place you're using it. I'd stick to the singleton approach - it is well proven and it is optimized for memory consumption. – Plamen G Apr 22 '15 at 08:10