Engauge Digitizer  2
Logger.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include <log4cpp/Category.hh>
8 #include <log4cpp/PatternLayout.hh>
9 #include <log4cpp/PropertyConfigurator.hh>
10 #include "Logger.h"
11 #include <log4cpp/RollingFileAppender.hh>
12 #include <QString>
13 
14 log4cpp::Category *mainCat;
15 
16 const QString INDENTATION_PAST_TIMESTAMP (" ");
17 const QString INDENTATION_DELTA (" ");
18 
19 void initializeLogging (const QString &name,
20  const QString &filename,
21  bool isDebug)
22 {
23  const size_t MAX_FILE_SIZE_BYTES = 6 * 1024 * 1024; // Size that should satisfy most email servers
24  const unsigned int MAX_BACKUP_INDEX = 2;
25  const bool APPEND_TO_PREVIOUS_FILE = false;
26 
27  log4cpp::RollingFileAppender *appender = new log4cpp::RollingFileAppender (name.toStdString (),
28  filename.toStdString (),
29  MAX_FILE_SIZE_BYTES,
30  MAX_BACKUP_INDEX,
31  APPEND_TO_PREVIOUS_FILE);
32 
33  log4cpp::PatternLayout *layout = new log4cpp::PatternLayout ();
34  layout->setConversionPattern ("%d{%H:%M:%S.%l} %-5p %c - %m%n");
35  appender->setLayout (layout);
36 
37  mainCat = &log4cpp::Category::getRoot ();
38 
39  // Levels are EMERG, FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG.
40  //
41  // Most trace logging is at INFO level, but methods that are called extremely often (like mouse
42  // moves and status bar updates) are at the lower DEBUG level so they are rarely seen
43  if (isDebug) {
44  mainCat->setPriority (log4cpp::Priority::DEBUG);
45  } else {
46  mainCat->setPriority (log4cpp::Priority::INFO);
47  }
48 
49  mainCat->addAppender (appender);
50 }