CFEL - ASG Software Suite  2.5.0
CASS
log.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 Lutz Foucar
2 
3 /**
4  * @file log.cpp contains logger for cass
5  *
6  * @author Lutz Foucar
7  */
8 
9 #include <iostream>
10 
11 #include <QtCore/QDateTime>
12 #include <QtCore/QString>
13 #include <QtCore/QFileInfo>
14 #include <QtCore/QDir>
15 #include <QtCore/QMutexLocker>
16 
17 #include "log.h"
18 
19 #include "cass_settings.h"
20 
21 using namespace cass;
22 using namespace std;
23 using namespace tr1;
24 
25 std::tr1::shared_ptr<Log> Log::_instance;
28 const char* Log::_level2string[] =
29 {"ERROR ","WARNING ","INFO ","VERBOSEINFO ","DEBUG ","DEBUG1 ","DEBUG2 ","DEBUG3 ","DEBUG4 "};
30 
31 void Log::add(Level level, const std::string& line)
32 {
33  if (_loggingLevel < level)
34  return;
35  QMutexLocker locker(&_lock);
36  if (!_instance)
37  _instance = std::tr1::shared_ptr<Log>(new Log());
38  _instance->addline(level,line);
39 }
40 
42 {
43  QMutexLocker locker(&_lock);
44  if (!_instance)
45  _instance = std::tr1::shared_ptr<Log>(new Log());
46  _instance->load();
47 }
48 
49 string Log::filename()
50 {
51  QMutexLocker locker(&_lock);
52  if (!_instance)
53  _instance = std::tr1::shared_ptr<Log>(new Log());
54  return _instance->_filename;
55 }
56 
58 {
59  load();
60 }
61 
62 void Log::load()
63 {
64  CASSSettings s;
65  s.beginGroup("Log");
66  for (int i(0); i < nbrOfLogLevel ; ++i)
67  {
68  if (s.value("MaxLoggingLevel","INFO").toString() + " " == _level2string[i])
69  {
70  _loggingLevel = static_cast<Level>(i);
71  break;
72  }
73  _loggingLevel = INFO;
74  }
75  QDir directory(s.value("Directory",QDir::currentPath()).toString());
76  QString tmpfilename("casslog_" +
77  QDateTime::currentDateTime().toString("yyyyMMdd") +
78  ".log");
79  QString filename(s.value("Filename",tmpfilename).toString());
80  QFileInfo fileinfo(directory,filename);
81  if(fileinfo.filePath().toStdString() != _filename)
82  {
83  if (_log.is_open())
84  _log.close();
85  _log.open(fileinfo.filePath().toUtf8().data(), ios_base::out | ios_base::app);
86  _filename = fileinfo.filePath().toStdString();
87  }
88 }
89 
91 {
92  _log.close();
93 }
94 
95 void Log::addline(Level level, const string &line)
96 {
97  _log << QDateTime::currentDateTime().toString("yyyy/MM/dd_HH:mm:ss.zzz ").toStdString()
98  << _level2string[level]
99  << line
100  << endl << flush;
101 }
void load()
load the logging settings from the .ini file
Definition: log.cpp:62
static void loadSettings()
load the logging settings from the .ini file
Definition: log.cpp:41
Log()
constructor
Definition: log.cpp:57
Settings for CASS.
Definition: cass_settings.h:30
Level
the logging levels available
Definition: log.h:39
static std::tr1::shared_ptr< Log > _instance
the instance
Definition: log.h:109
STL namespace.
currentPath()
static Level _loggingLevel
the used logging level
Definition: log.h:124
static void add(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:31
static const char * _level2string[]
map the level to a string
Definition: log.h:127
static QMutex _lock
mutex to lock the singleton
Definition: log.h:121
a logger for logging debug and info messages
Definition: log.h:35
std::string toString(const Type &t)
convert any type to a string
Definition: cass.h:63
value(const QString &key, const QVariant &defaultValue=QVariant()
void addline(Level level, const std::string &line)
add a string to the log
Definition: log.cpp:95
~Log()
destructor
Definition: log.cpp:90
file contains specialized class that do the settings for cass
static std::string filename()
return the name of the log file
Definition: log.cpp:49
contains a logger for cass
beginGroup(const QString &prefix)