source: sources/src/toolsLogger.cc @ 1500:872b0b69e0d5

Revision 1500:872b0b69e0d5, 4.1 KB checked in by niam, 12 months ago (diff)

{issue #103} added implementation of sl- and dl- lists

Line 
1/***************************************************************************
2 *            toolsLogger.cc
3 *
4 *  Fri Mar 21 2008
5 *  Copyright  2008  Dmytro Milinevskyy
6 *  milinevskyy@gmail.com
7 ****************************************************************************/
8
9/*
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License version 2.1 as published by
12 *  the Free Software Foundation;
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU Library General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/**
25 * vim indentation settings
26 * set tabstop=4
27 * set shiftwidth=4
28 */
29
30#include <libdodo/directives.h>
31
32#include <syslog.h>
33
34#include <libdodo/toolsLogger.h>
35
36#include <libdodo/ioChannel.h>
37#include <libdodo/toolsTime.h>
38#include <libdodo/pcSyncProcess.h>
39#include <libdodo/pcSyncStack.h>
40#include <libdodo/types.h>
41
42using namespace dodo::tools;
43
44const dodo::string logger::levels[] = {
45    "INFO",
46    "NOTICE",
47    "DEBUG",
48    "WARNING",
49    "ERROR",
50    "ALERT",
51    "CRITICAL",
52    "EMERGENCY",
53    "USER"
54};
55
56//-------------------------------------------------------------------
57
58const int logger::syslogLevels[] = {
59    LOG_EMERG,
60    LOG_ALERT,
61    LOG_CRIT,
62    LOG_ERR,
63    LOG_WARNING,
64    LOG_NOTICE,
65    LOG_INFO,
66    LOG_DEBUG,
67    LOG_USER
68};
69
70//-------------------------------------------------------------------
71
72logger::logger() : forward(false),
73                   timeFormat(" %d/%m/%Y.%H-%M-%S: "),
74                   handlersNum(0),
75                   keeper(new pc::sync::process(0))
76{
77}
78
79//-------------------------------------------------------------------
80
81logger::~logger()
82{
83    delete keeper;
84}
85
86//-------------------------------------------------------------------
87
88unsigned long
89logger::add(short       level,
90            io::channel *handler)
91{
92    pc::sync::stack pg(keeper);
93
94    __log_map__ lm;
95
96    lm.handler = handler;
97    lm.level = level;
98    lm.position = ++handlersNum;
99
100    handlers.push(lm);
101
102    return handlersNum;
103}
104
105//-------------------------------------------------------------------
106
107void
108logger::remove(unsigned long position)
109{
110    pc::sync::stack pg(keeper);
111
112    dodo::slList<__log_map__>::iterator i(handlers.begin()), j(handlers.end());
113    for (; i != j; ++i) {
114        if (i->position == position) {
115            handlers.remove(i);
116
117            break;
118        }
119    }
120}
121
122//-------------------------------------------------------------------
123
124void
125logger::log(short            level,
126            const dodo::string &msg)
127{
128    pc::sync::stack pg(keeper);
129
130    if (level < 0 && level >= LOG_LEVEL_ENUMSIZE)
131        return;
132
133    dodo::slList<__log_map__>::iterator i(handlers.begin()), j(handlers.end());
134    for (; i != j; ++i) {
135        if (i->level == level) {
136            if (i->handler != NULL) {
137                i->handler->writeString(levels[level] + tools::time::byFormat(timeFormat, tools::time::now()) + msg + "\n");
138                i->handler->flush();
139            } else
140                syslog(syslogLevels[level], "%s", msg.data());
141        }
142    }
143
144    if (forward) {
145        i = instance().handlers.begin();
146        j = instance().handlers.end();
147        for (; i != j; ++i) {
148            if (i->level == level) {
149                if (i->handler != NULL) {
150                    i->handler->writeString(levels[level] + tools::time::byFormat(timeFormat, tools::time::now()) + msg + "\n");
151                    i->handler->flush();
152                } else
153                    syslog(syslogLevels[level], "%s", msg.data());
154            }
155        }
156    }
157}
158
159//-------------------------------------------------------------------
160
161void
162logger::setTimeFormat(const dodo::string &format)
163{
164    timeFormat = " " + format + ": ";
165}
166
167//-------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.