1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * Pan - A Newsreader for Gtk+
4  * Copyright (C) 2002-2006  Charles Kerr <charles@rebelbase.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include <config.h>
21 #include <iostream>
22 #include <cstdarg>
23 #include <cstdio> // vsnprintf
24 #include "log.h"
25 
26 using namespace pan;
27 
28 /***
29 ***/
30 
31 void
clear()32 Log :: clear ()
33 {
34    _entries.clear ();
35    fire_cleared ();
36 }
37 
38 /***
39 ***/
40 
41 void
fire_entry_added(const Entry & e)42 Log :: fire_entry_added (const Entry& e) {
43   for (listeners_t::const_iterator i(_listeners.begin()), end(_listeners.end()); i!=end; )
44     (*i++)->on_log_entry_added (e);
45 }
46 
47 void
fire_cleared()48 Log :: fire_cleared () {
49   for (listeners_t::const_iterator i(_listeners.begin()), end(_listeners.end()); i!=end; )
50     (*i++)->on_log_cleared ();
51 }
52 
53 /***
54 ***/
55 
56 void
add_entry(Entry & e,std::deque<Entry> & list)57 Log :: add_entry(Entry& e, std::deque<Entry>& list)
58 {
59   _entries.resize (_entries.size() + 1);
60   Entry& a (_entries.back());
61   a.date = time(NULL);
62   a.severity = e.severity;
63   a.message = e.message;
64   foreach (std::deque<Entry>, list, it)
65   {
66     Entry* new_entry = new Entry(*it);
67     a.messages.push_back(new_entry);
68   }
69   fire_entry_added (a);
70 }
71 
72 void
add(Severity severity,const char * msg)73 Log :: add (Severity severity, const char * msg)
74 {
75   _entries.resize (_entries.size() + 1);
76   Entry& e (_entries.back());
77   e.date = time(NULL);
78   e.severity = severity;
79   e.message = msg;
80   fire_entry_added (e);
81 }
82 
83 void
add_va(Severity severity,const char * fmt,...)84 Log :: add_va (Severity severity, const char * fmt, ...)
85 {
86    if (fmt != NULL)
87    {
88       va_list args;
89       va_start (args, fmt);
90       char buf[4096];
91       vsnprintf (buf, sizeof(buf), fmt, args);
92       va_end (args);
93       add (severity, buf);
94    }
95 }
96 
97 void
add_info_va(const char * fmt,...)98 Log :: add_info_va (const char * fmt, ...)
99 {
100   va_list args;
101   va_start (args, fmt);
102   char buf[4096];
103   vsnprintf (buf, sizeof(buf), fmt, args);
104   va_end (args);
105 
106   add_info (buf);
107 }
108 
109 void
add_err_va(const char * fmt,...)110 Log :: add_err_va (const char * fmt, ...)
111 {
112   va_list args;
113   va_start (args, fmt);
114   char buf[4096];
115   vsnprintf (buf, sizeof(buf), fmt, args);
116   va_end (args);
117 
118   add_err (buf);
119 }
120 
121 void
add_urgent_va(const char * fmt,...)122 Log :: add_urgent_va (const char * fmt, ...)
123 {
124   va_list args;
125   va_start (args, fmt);
126   char buf[4096];
127   vsnprintf (buf, sizeof(buf), fmt, args);
128   va_end (args);
129 
130   add_urgent (buf);
131 }
132