/src/dcmtk/oflog/libsrc/loglog.cc
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: loglog.cxx |
3 | | // Created: 6/2001 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2001-2010 Tad E. Smith |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | // you may not use this file except in compliance with the License. |
11 | | // You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software |
16 | | // distributed under the License is distributed on an "AS IS" BASIS, |
17 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | // See the License for the specific language governing permissions and |
19 | | // limitations under the License. |
20 | | |
21 | | #include "dcmtk/oflog/streams.h" |
22 | | #include "dcmtk/oflog/helpers/loglog.h" |
23 | | #include "dcmtk/oflog/thread/syncpub.h" |
24 | | #include "dcmtk/oflog/thread/threads.h" |
25 | | #include "dcmtk/oflog/internal/env.h" |
26 | | #include "dcmtk/oflog/consap.h" |
27 | | #include <ostream> |
28 | | #include <stdexcept> |
29 | | |
30 | | |
31 | | namespace dcmtk { |
32 | | namespace log4cplus { namespace helpers { |
33 | | |
34 | | namespace |
35 | | { |
36 | | |
37 | | static tchar const PREFIX[] = DCMTK_LOG4CPLUS_TEXT("log4cplus: "); |
38 | | static tchar const WARN_PREFIX[] = DCMTK_LOG4CPLUS_TEXT("log4cplus:WARN "); |
39 | | static tchar const ERR_PREFIX[] = DCMTK_LOG4CPLUS_TEXT("log4cplus:ERROR "); |
40 | | |
41 | | } // namespace |
42 | | |
43 | | |
44 | | LogLog * |
45 | | LogLog::getLogLog() |
46 | 0 | { |
47 | 0 | return &helpers::getLogLog (); |
48 | 0 | } |
49 | | |
50 | | |
51 | | LogLog::LogLog() |
52 | 3 | : debugEnabled(TriUndef) |
53 | 3 | , quietMode(TriUndef) |
54 | 3 | , mutex() |
55 | 3 | { } |
56 | | |
57 | | |
58 | | LogLog::~LogLog() |
59 | 0 | { } |
60 | | |
61 | | |
62 | | void |
63 | | LogLog::setInternalDebugging(bool enabled) |
64 | 0 | { |
65 | 0 | thread::MutexGuard guard (mutex); |
66 | |
|
67 | 0 | debugEnabled = enabled ? TriTrue : TriFalse; |
68 | 0 | } |
69 | | |
70 | | |
71 | | void |
72 | | LogLog::setQuietMode(bool quietModeVal) |
73 | 0 | { |
74 | 0 | thread::MutexGuard guard (mutex); |
75 | |
|
76 | 0 | quietMode = quietModeVal ? TriTrue : TriFalse; |
77 | 0 | } |
78 | | |
79 | | |
80 | | void |
81 | | LogLog::debug(const log4cplus::tstring& msg) const |
82 | 0 | { |
83 | 0 | logging_worker (tcout, &LogLog::get_debug_mode, PREFIX, msg.c_str()); |
84 | 0 | } |
85 | | |
86 | | |
87 | | void |
88 | | LogLog::debug(tchar const * msg) const |
89 | 0 | { |
90 | 0 | logging_worker (tcout, &LogLog::get_debug_mode, PREFIX, msg); |
91 | 0 | } |
92 | | |
93 | | |
94 | | void |
95 | | LogLog::warn(const log4cplus::tstring& msg) const |
96 | 0 | { |
97 | 0 | logging_worker (tcerr, &LogLog::get_not_quiet_mode, WARN_PREFIX, msg.c_str()); |
98 | 0 | } |
99 | | |
100 | | |
101 | | void |
102 | | LogLog::warn(tchar const * msg) const |
103 | 0 | { |
104 | 0 | logging_worker (tcerr, &LogLog::get_not_quiet_mode, WARN_PREFIX, msg); |
105 | 0 | } |
106 | | |
107 | | |
108 | | void |
109 | | LogLog::error(const log4cplus::tstring& msg, bool throw_flag) const |
110 | 0 | { |
111 | 0 | logging_worker (tcerr, &LogLog::get_not_quiet_mode, ERR_PREFIX, msg.c_str(), |
112 | 0 | throw_flag); |
113 | 0 | } |
114 | | |
115 | | |
116 | | void |
117 | | LogLog::error(tchar const * msg, bool throw_flag) const |
118 | 0 | { |
119 | 0 | logging_worker (tcerr, &LogLog::get_not_quiet_mode, ERR_PREFIX, msg, |
120 | 0 | throw_flag); |
121 | 0 | } |
122 | | |
123 | | |
124 | | bool |
125 | | LogLog::get_quiet_mode () const |
126 | 0 | { |
127 | 0 | if (quietMode == TriUndef) |
128 | 0 | set_tristate_from_env (&quietMode, |
129 | 0 | DCMTK_LOG4CPLUS_TEXT ("DCMTK_LOG4CPLUS_LOGLOG_QUIETMODE")); |
130 | |
|
131 | 0 | return quietMode == TriTrue; |
132 | 0 | } |
133 | | |
134 | | |
135 | | bool |
136 | | LogLog::get_not_quiet_mode () const |
137 | 0 | { |
138 | 0 | return ! get_quiet_mode (); |
139 | 0 | } |
140 | | |
141 | | |
142 | | bool |
143 | | LogLog::get_debug_mode () const |
144 | 0 | { |
145 | 0 | if (debugEnabled == TriUndef) |
146 | 0 | set_tristate_from_env (&debugEnabled, |
147 | 0 | DCMTK_LOG4CPLUS_TEXT ("DCMTK_LOG4CPLUS_LOGLOG_DEBUGENABLED")); |
148 | |
|
149 | 0 | return debugEnabled && ! get_quiet_mode (); |
150 | 0 | } |
151 | | |
152 | | |
153 | | void |
154 | | LogLog::set_tristate_from_env (TriState * result, tchar const * envvar_name) |
155 | 0 | { |
156 | 0 | tstring envvar_value; |
157 | 0 | bool exists = internal::get_env_var (envvar_value, envvar_name); |
158 | 0 | bool value = false; |
159 | 0 | if (exists && internal::parse_bool (value, envvar_value) && value) |
160 | 0 | *result = TriTrue; |
161 | 0 | else |
162 | 0 | *result = TriFalse; |
163 | 0 | } |
164 | | |
165 | | |
166 | | void |
167 | | LogLog::logging_worker (tostream & os, bool (LogLog:: * cond) () const, |
168 | | tchar const * prefix, tchar const * msg, bool throw_flag) const |
169 | 0 | { |
170 | 0 | bool output; |
171 | 0 | { |
172 | 0 | thread::MutexGuard guard (mutex); |
173 | 0 | output = (this->*cond) (); |
174 | 0 | } |
175 | |
|
176 | 0 | if (DCMTK_LOG4CPLUS_UNLIKELY (output)) |
177 | 0 | { |
178 | | // XXX This is potential recursive lock of |
179 | | // ConsoleAppender::outputMutex. |
180 | 0 | thread::MutexGuard outputGuard (ConsoleAppender::getOutputMutex ()); |
181 | 0 | os << prefix << msg << OFendl; |
182 | 0 | } |
183 | |
|
184 | 0 | if (DCMTK_LOG4CPLUS_UNLIKELY (throw_flag)) |
185 | 0 | throw STD_NAMESPACE runtime_error (DCMTK_LOG4CPLUS_TSTRING_TO_STRING (msg)); |
186 | 0 | } |
187 | | |
188 | | |
189 | | } } // namespace log4cplus { namespace helpers { |
190 | | } // end namespace dcmtk |