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 using namespace log4cpp;
20 
21 void initializeLogging (const QString &name,
22  const QString &filename,
23  bool isDebug)
24 {
25  LayoutAppender *appender = 0;
26 
27  const size_t MAX_FILE_SIZE_BYTES = 6 * 1024 * 1024; // Size that should satisfy most email servers
28  const unsigned int MAX_BACKUP_INDEX = 2;
29  const bool APPEND_TO_PREVIOUS_FILE = false;
30 
31  // Log to file for development
32  appender = dynamic_cast<LayoutAppender*> (new RollingFileAppender (name.toStdString (),
33  filename.toStdString (),
34  MAX_FILE_SIZE_BYTES,
35  MAX_BACKUP_INDEX,
36  APPEND_TO_PREVIOUS_FILE));
37 
38  PatternLayout *layout = new PatternLayout ();
39  layout->setConversionPattern ("%d{%H:%M:%S.%l} %-5p %c - %m%n");
40  appender->setLayout (layout);
41 
42  mainCat = &Category::getRoot ();
43 
44  // Levels are EMERG, FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG.
45  //
46  // Most trace logging is at INFO level, but methods that are called extremely often (like mouse
47  // moves and status bar updates) are at the lower DEBUG level so they are rarely seen
48  if (isDebug) {
49  mainCat->setPriority (Priority::DEBUG);
50  } else {
51  mainCat->setPriority (Priority::INFO);
52  }
53 
54  mainCat->addAppender (appender);
55 }