source: trash/old-modules/transport/protlib/logfile.cpp@ 5887

Last change on this file since 5887 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 5.0 KB
Line 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file logfile.cpp
3/// Implementation of a logging stream
4/// ----------------------------------------------------------
5/// $Id: logfile.cpp 2549 2007-04-02 22:17:37Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/src/logfile.cpp $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29#include "logfile.h"
30#include <iostream>
31#include <iomanip>
32#include <new>
33
34#include <time.h>
35#include <sys/time.h> // gettimeofday
36
37namespace protlib {
38
39using namespace std;
40
41 namespace log {
42
43const char *const ANSIcolorcode[]= {
44 "[0m", // clear, off; reset; clears all colors and styles (to white on black)
45 "[1m", // bold_on
46 "[3m", // italics_on
47 "[4m", // underline_on
48 "[7m", // inverse_on
49 "[9m", // strikethrough_on
50 "[22m", // bold_off
51 "[23m", // italics_off
52 "[24m", // underline_off
53 "[27m", // inverse_off
54 "[29m", // strikethrough_off
55 "[30m", // black
56 "[31m", // red
57 "[32m", // green
58 "[33m", // yellow
59 "[34m", // blue
60 "[35m", // magenta
61 "[36m", // cyan
62 "[37m", // white
63 "[39m", // default
64 "[40m", // bg_black
65 "[41m", // bg_red
66 "[42m", // bg_green
67 "[43m", // bg_yellow
68 "[44m", // bg_blue
69 "[45m", // bg_magenta
70 "[46m", // bg_cyan
71 "[47m", // bg_white
72 "[49m" // bg_default
73};
74
75const char* color[num_colors+1];
76
77const char *const logclass_str[]=
78{
79 " ZERO ",
80 "*ERROR*",
81 "WARNING",
82 " EVENT ",
83 " INFO ",
84 " DEBUG ",
85 " EXPERT"
86};
87
88
89const char *const logseveritylevel_str[]=
90 {
91 "EMERG ",
92 "ALERT ",
93 "CRITIC",
94 "LEVEL3",
95 "NORMAL",
96 "LEVEL5",
97 "LEVEL6",
98 "LEVEL7",
99 "UNIMP ",
100 "LEVEL9",
101 "LEVELA",
102 "LEVELB",
103 "LEVELC",
104 "LEVELD",
105 "LEVELE",
106 "-ALL- "
107 };
108
109
110/** set logging destination to new filename
111 * @return true if logfile could be opened for given name
112 */
113bool
114logfile::set_dest(const char* filename, bool quiet)
115{
116 // lock everything
117 pthread_mutex_lock(&logmutex);
118
119 if (logstream && !quiet)
120 {
121// (*logstream) << color[blue] << timenow() << '[' << getpid() << "] Redirecting Log output to \"" << filename << '\"' << endl;
122// (*logstream) << color[blue] << timenow()
123// << '[' << getpid() << "] <<<<<<<<<<<<<<<<<<<<<<<< *** LOG STOP *** <<<<<<<<<<<<<<<<<<<<<<<<"
124// << color[off] << endl;
125 }
126
127 // delete old stream
128 if (logstream!= &cout)
129 delete logstream;
130
131 // allocate new stream
132 if (strlen(filename))
133 logstream= new(nothrow) ofstream(filename);
134 else
135 logstream= &cout;
136
137 pthread_mutex_unlock(&logmutex);
138
139 if (!logstream)
140 {
141 cerr << "Could not open logfile " << filename << endl;
142 return false;
143 }
144 else
145 {
146// (*logstream) << color[blue] << timenow()
147// << '[' << getpid() << "] >>>>>>>>>>>>>>>>>>>>>>>> *** LOG START *** >>>>>>>>>>>>>>>>>>>>>>>>"
148// << color[off] << endl;
149 }
150 return true;
151}
152
153
154ostream&
155logfile::logstart(logclass_t logclass, loglevel_t severity_level,
156 const string& modname,
157 const char* file,
158 const char* func,
159 int line)
160{
161 // lock logstream for writing, must be unlocked by logfile.end()
162 int mtxlck_ret= 0;
163 if ( (mtxlck_ret= pthread_mutex_lock(&logmutex)) )
164 {
165 cerr << color[red] << "logfile::logstart() ERROR while locking mutex - return code:" << mtxlck_ret << "/" << strerror(mtxlck_ret) << endl;
166 }
167
168 if ( logstream )
169 {
170 (*logstream) << (logclass==ERROR_LOG ? color[bold_on] : "")
171 << (logclass==WARNING_LOG ? color[magenta] :
172 (logclass==ERROR_LOG ? color[red] : color[off])
173 )
174 << timenow()
175 << '-' << getpid() << (logclass!=ERROR_LOG ? '-' : '*')
176 << color[bold_on] << logclass_str[logclass>>4] << (logclass==ERROR_LOG ? color[bold_on] : color[bold_off])
177 << (logclass!=ERROR_LOG ? '/' : '*') << hex << severity_level << dec
178 << ": " << color[bold_on] << left << setfill(' ') << setw(15) << modname << color[bold_off] << right << " " << color[off];
179 }
180
181 return (*logstream);
182}
183
184
185 } // end namespace log
186
187} // end namespace protlib
Note: See TracBrowser for help on using the repository browser.