1b89a93b6f7bc9d273832e2a49beb7631c31e9a3
[platform/upstream/syncevolution.git] / src / syncevo / Logging.cpp
1 /*
2  * Copyright (C) 2009 Patrick Ohly <patrick.ohly@gmx.de>
3  * Copyright (C) 2009 Intel Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) version 3.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301  USA
19  */
20
21 #include <syncevo/Logging.h>
22 #include <syncevo/LogStdout.h>
23
24 #include <vector>
25
26 #include <syncevo/declarations.h>
27 SE_BEGIN_CXX
28
29 static LoggerStdout DefaultLogger;
30
31 static std::vector<LoggerBase *> loggers;
32 LoggerBase &LoggerBase::instance()
33 {
34     if (!loggers.empty()) {
35         return *loggers[loggers.size() - 1];
36     } else {
37         return DefaultLogger;
38     }
39 }
40
41 void LoggerBase::pushLogger(LoggerBase *logger)
42 {
43     loggers.push_back(logger);
44 }
45
46 void LoggerBase::popLogger()
47 {
48     if (loggers.empty()) {
49         throw "too many popLogger() calls";
50     } else {
51         loggers.pop_back();
52     }
53 }
54
55 void Logger::message(Level level,
56                      const char *prefix,
57                      const char *file,
58                      int line,
59                      const char *function,
60                      const char *format,
61                      ...)
62 {
63     va_list args;
64     va_start(args, format);
65     messagev(level, prefix, file, line, function, format, args);
66     va_end(args);
67 }
68
69 const char *Logger::levelToStr(Level level)
70 {
71     switch (level) {
72     case ERROR: return "ERROR";
73     case WARNING: return "WARNING";
74     case INFO: return "INFO";
75     case DEV: return "DEVELOPER";
76     case DEBUG: return "DEBUG";
77     default: return "???";
78     }
79 }
80
81 SE_END_CXX