Question

I'm diving through ACE and, I'm logging message in a file using the ACE_ERROR macro. And AFAIK, ACE_ERROR logs all the messages in the same file, regardless of their error level.

However, I actually need to write messages, according to their error level. I did see the ACE_LOG_MSG->open() function however, what i understand is that when you already calling this function twice, the second time it will close the file you opened when you called the function at the beginning.

Suppose I have a list and I want to log it and, in this list, two adjacent items don't have the same error level.

Then I would be opening and closing files, wouldn't than affect my apps performance ?

So is there a way to keep those files open ? Thanks !

Was it helpful?

Solution

Not closing the files you log to is particularly bad in debugging. If the application crashes with an open file, its contents may (and that happens rather often) get corrupted, leaving you with absolutely no information.

If you close the file properly, though, you're guaranteed to find at least some info there, possibly closer to the real issue. If you are concerned with performance, you should simply reduce log level, or if it's not feasible, you could perhaps offload the logging to the other process via (for example) TCP connection.

Anyway, don't optimize until you've measured! It might just be there'll be no impact, performance is a complicated problem which depends on lot of factors.

OTHER TIPS

Another example to re-direct logging as per their logging priority, using a simple wrapper class.

Hope this is useful to someone.

Example program

#include "ace/Log_Msg.h"
#include "ace/streams.h"

// @Author:  Gaurav A
// @Date:    2019OCT11
//
// Log each logging statement
// in file based on its priority
//
// eg: INFO logs goes to INFO.log
//     DEBUG logs goes to DEBUG.log

class Logger
{
  private:
    ACE_OSTREAM_TYPE* m_infolog=nullptr;
    ACE_OSTREAM_TYPE* m_debuglog=nullptr;
  public:
    Logger(void)
    : m_infolog (new std::ofstream ("INFO.log")),
      m_debuglog (new std::ofstream ("DEBUG.log"))
    {
    }
    ~Logger(void)
    {
      delete m_infolog;
      delete m_debuglog;
    }
    int log (ACE_Log_Priority p,
             const ACE_TCHAR* fmt,
             ...)
    {
      ssize_t final_result=0;

      if (p == LM_DEBUG)
      {
        va_list argp;
        va_start (argp, fmt);
        ACE_LOG_MSG->msg_ostream (m_debuglog);
        ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
        final_result = ACE_LOG_MSG->log (fmt, LM_DEBUG, argp);
        va_end (argp);
      }
      else if (p == LM_INFO)
      {
        va_list argp;
        va_start (argp, fmt);
        ACE_LOG_MSG->msg_ostream (m_infolog);
        ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
        final_result = ACE_LOG_MSG->log (fmt, LM_INFO, argp);
        va_end (argp);
      }

      return final_result;
    }
};

int
ACE_TMAIN (void)
{
  Logger logger;

  logger.log (LM_DEBUG, "I am a debug message no %d\n", 1);
  logger.log (LM_INFO,  "I am a info message no %d\n",  2);
  logger.log (LM_DEBUG, "I am a debug message no %d\n", 3);
  logger.log (LM_INFO,  "I am a info message no %d\n",  4);

  return 0;
}

Sample Output

[07:59:10]Host@User:~/acedir
$:  ./logging_each_priority_in_its_own_file
I am a debug message no 1
I am a info message no 2
I am a debug message no 3
I am a info message no 4
[07:59:10]Host@User:~/acedir
$:  ls -lrth
total 464K
-rw-r--r-- 1 aryaaur devusers  231 Oct 11 07:09 logging_each_priority_in_its_own_file.mpc
-rw-r--r-- 1 aryaaur devusers 5.6K Oct 11 07:29 GNUmakefile.logging_each_priority_in_its_own_file
-rw-r--r-- 1 aryaaur devusers 1.5K Oct 11 07:47 main_logging_each_priority_in_its_own_file_20191011.cpp
-rwxr-xr-x 1 aryaaur devusers  65K Oct 11 07:47 logging_each_priority_in_its_own_file
-rw-r--r-- 1 aryaaur devusers   50 Oct 11 07:59 INFO.log
-rw-r--r-- 1 aryaaur devusers   52 Oct 11 07:59 DEBUG.log
[07:59:10]Host@User:~/acedir
$:  cat INFO.log
I am a info message no 2
I am a info message no 4
[07:59:10]Host@User:~/acedir
$:  cat DEBUG.log
I am a debug message no 1
I am a debug message no 3
[07:59:10]Host@User:~/acedir
$:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top