Line data Source code
1 : /*
2 : * (C) Copyright 2010 Marek Dopiera
3 : *
4 : * This file is part of CoherentDB.
5 : *
6 : * CoherentDB is free software: you can redistribute it and/or modify it
7 : * under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation, either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * CoherentDB is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public
17 : * License along with CoherentDB. If not, see
18 : * http://www.gnu.org/licenses/.
19 : */
20 :
21 : #include <sstream>
22 : #include <cassert>
23 : #include <cstring>
24 :
25 : #include <log/log.h>
26 :
27 : #include <log4cxx/fileappender.h>
28 : #include <log4cxx/patternlayout.h>
29 :
30 : using namespace std;
31 : using namespace log4cxx;
32 :
33 : namespace coherent {
34 : namespace log {
35 :
36 7 : LevelPtr log_FATAL(Level::getFatal());
37 7 : LevelPtr log_ERROR(Level::getError());
38 7 : LevelPtr log_WARN(Level::getWarn());
39 7 : LevelPtr log_INFO(Level::getInfo());
40 7 : LevelPtr log_DEBUG(Level::getDebug());
41 7 : LevelPtr log_TRACE(Level::getTrace());
42 7 : LevelPtr global_log_limit;
43 :
44 : //we want to convert pretty function to the format
45 : //namespace1.namespace2.class1.class2
46 20036 : void convert_pretty_function(char const * s, char * const out)
47 : {
48 20036 : size_t out_off = 0;
49 20036 : size_t in_templ = 0; //nest in template
50 20036 : size_t last_off = 0;
51 20036 : assert(s);
52 923252 : for (; s[0] != '('; ++s) {
53 903221 : assert(s[0]);
54 :
55 903216 : if (in_templ) {
56 552 : if (s[0] == '<')
57 0 : ++in_templ;
58 552 : else if (s[0] == '>') {
59 280 : assert(in_templ);
60 280 : --in_templ;
61 : }
62 552 : continue;
63 : }
64 902664 : if (s[0] == '<') {
65 : //check if it is not operatror< also start a template, but nex char
66 : //has to be '(' which will successfully break the loop
67 280 : ++in_templ;
68 280 : continue;
69 902384 : } else if (s[0] == ':') {
70 80141 : last_off = out_off;
71 80141 : out[out_off++] = '.';
72 80141 : ++s;
73 80141 : assert(s[0] == ':');
74 80141 : continue;
75 : }
76 822243 : if (s[0] == ' ') { //return value separator
77 19021 : out_off = 0;
78 19021 : last_off = 0;
79 19021 : continue;
80 : }
81 803222 : out[out_off++] = s[0];
82 : }
83 20031 : out[last_off] = 0;
84 20031 : }
85 :
86 7 : static void setup_logger(string const & log_path, LevelPtr const & log_level,
87 : LevelPtr const & global_log_limit_arg)
88 : {
89 14 : LoggerPtr root_logger = Logger::getRootLogger();
90 7 : root_logger->setLevel(log_level);
91 : char buf[sizeof(LevelPtr)];
92 :
93 14 : LevelPtr gl2(global_log_limit_arg);
94 7 : memcpy(buf, &gl2, sizeof(global_log_limit_arg));
95 7 : memcpy(&gl2, &global_log_limit, sizeof(global_log_limit));
96 7 : memcpy(&global_log_limit, buf, sizeof(global_log_limit));
97 : // global_log_limit = global_log_limit_arg; <--- should be that way but log4cxx
98 : // is ugly ano compiler complains
99 :
100 7 : root_logger->addAppender(
101 : new FileAppender(
102 14 : new PatternLayout("%8r %5p %m [%c (%F:%L)]%n"),
103 : log_path,
104 : false,
105 : true,
106 : 8192
107 14 : )
108 21 : );
109 7 : }
110 :
111 7 : void setup_logger_test(string const & log_path, LevelPtr const & log_level)
112 : {
113 7 : setup_logger(log_path, log_level, Level::getTrace());
114 7 : }
115 :
116 0 : void setup_logger_prod(string const & log_path)
117 : {
118 : #ifndef NDEBUG
119 0 : setup_logger(log_path, Level::getDebug(), Level::getTrace());
120 : #else
121 : setup_logger(log_path, Level::getInfo(), Level::getInfo());
122 : #endif
123 0 : }
124 :
125 0 : void flush_logger()
126 : {
127 0 : AppenderList apps = log4cxx::Logger::getRootLogger()->getAllAppenders();
128 0 : for (AppenderList::iterator i = apps.begin(); i != apps.end(); ++i) {
129 0 : FileAppender * app = dynamic_cast<FileAppender *>(&(**i));
130 0 : if (app)
131 0 : app->setImmediateFlush(true);
132 : }
133 0 : }
134 :
135 : } // namespace log
136 21 : } // namespace coherent
|