Code

add INTERACTION to filters in the environment variable
[inkscape.git] / src / debug / logger.cpp
1 /*
2  * Inkscape::Debug::Logger - debug logging facility
3  *
4  * Authors:
5  *   MenTaLguY <mental@rydia.net>
6  *
7  * Copyright (C) 2005 MenTaLguY
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #include <fstream>
13 #include <vector>
14 #include <glib/gmessages.h>
15 #include "debug/logger.h"
16 #include "debug/simple-event.h"
17 #include "gc-alloc.h"
19 namespace Inkscape {
21 namespace Debug {
23 bool Logger::_enabled=false;
24 bool Logger::_category_mask[Event::N_CATEGORIES];
26 namespace {
28 static void write_escaped_value(std::ostream &os, Util::ptr_shared<char> value) {
29     for ( char const *current=value ; *current ; ++current ) {
30         switch (*current) {
31         case '&':
32             os << "&amp;";
33             break;
34         case '"':
35             os << "&quot;";
36             break;
37         case '\'':
38             os << "&apos;";
39             break;
40         case '<':
41             os << "&lt;";
42             break;
43         case '>':
44             os << "&gt;";
45             break;
46         default:
47             os.put(*current);
48         }
49     }
50 }
52 static void write_indent(std::ostream &os, unsigned depth) {
53     for ( unsigned i = 0 ; i < depth ; i++ ) {
54         os.write("  ", 2);
55     }
56 }
58 static std::ofstream log_stream;
59 static bool empty_tag=false;
60 typedef std::vector<Util::ptr_shared<char>, GC::Alloc<Util::ptr_shared<char>, GC::MANUAL> > TagStack;
61 static TagStack &tag_stack() {
62     static TagStack stack;
63     return stack;
64 }
66 static void do_shutdown() {
67     Debug::Logger::shutdown();
68 }
70 static bool equal_range(char const *c_string,
71                         char const *start, char const *end)
72 {
73     return !std::strncmp(start, c_string, end - start) &&
74            !c_string[end - start];
75 }
77 static void set_category_mask(bool * const mask, char const *filter) {
78     if (!filter) {
79         for ( unsigned i = 0 ; i < Event::N_CATEGORIES ; i++ ) {
80             mask[i] = true;
81         }
82         return;
83     } else {
84         for ( unsigned i = 0 ; i < Event::N_CATEGORIES ; i++ ) {
85             mask[i] = false;
86         }
87         mask[Event::CORE] = true;
88     }
90     char const *start;
91     char const *end;
92     start = end = filter;
93     while (*end) {
94         while ( *end && *end != ',' ) { end++; }
95         if ( start != end ) {
96             struct CategoryName {
97                 char const *name;
98                 Event::Category category;
99             };
100             static const CategoryName category_names[] = {
101                 { "CORE", Event::CORE },
102                 { "XML", Event::XML },
103                 { "SPOBJECT", Event::SPOBJECT },
104                 { "DOCUMENT", Event::DOCUMENT },
105                 { "REFCOUNT", Event::REFCOUNT },
106                 { "EXTENSION", Event::EXTENSION },
107                 { "FINALIZERS", Event::FINALIZERS },
108                 { "INTERACTION", Event::INTERACTION },
109                 { "OTHER", Event::OTHER },
110                 { NULL, Event::OTHER }
111             };
112             CategoryName const *iter;
113             for ( iter = category_names ; iter->name ; iter++ ) {
114                 if (equal_range(iter->name, start, end)) {
115                     mask[iter->category] = true;
116                     break;
117                 }
118             }
119             if (!iter->name) {
120                 g_warning("Unknown debugging category %*s", (int)(end - start), start);
121             }
122         }
123         if (*end) {
124             start = end = end + 1;
125         }
126     }
131 void Logger::init() {
132     if (!_enabled) {
133         char const *log_filename=std::getenv("INKSCAPE_DEBUG_LOG");
134         if (log_filename) {
135             log_stream.open(log_filename);
136             if (log_stream.is_open()) {
137                 char const *log_filter=std::getenv("INKSCAPE_DEBUG_FILTER");
138                 set_category_mask(_category_mask, log_filter);
139                 log_stream << "<?xml version=\"1.0\"?>\n";
140                 log_stream.flush();
141                 _enabled = true;
142                 start<SimpleEvent<Event::CORE> >(Util::share_static_string("session"));
143                 std::atexit(&do_shutdown);
144             }
145         }
146     }
149 void Logger::_start(Event const &event) {
150     Util::ptr_shared<char> name=event.name();
152     if (empty_tag) {
153         log_stream << ">\n";
154     }
156     write_indent(log_stream, tag_stack().size());
158     log_stream << "<" << name.pointer();
160     unsigned property_count=event.propertyCount();
161     for ( unsigned i = 0 ; i < property_count ; i++ ) {
162         Event::PropertyPair property=event.property(i);
163         log_stream << " " << property.name.pointer() << "=\"";
164         write_escaped_value(log_stream, property.value);
165         log_stream << "\"";
166     }
168     log_stream.flush();
170     tag_stack().push_back(name);
171     empty_tag = true;
174 void Logger::_skip() {
175     tag_stack().push_back(Util::ptr_shared<char>());
178 void Logger::_finish() {
179     if (tag_stack().back()) {
180         if (empty_tag) {
181             log_stream << "/>\n";
182         } else {
183             write_indent(log_stream, tag_stack().size() - 1);
184             log_stream << "</" << tag_stack().back().pointer() << ">\n";
185         }
186         log_stream.flush();
188         empty_tag = false;
189     }
191     tag_stack().pop_back();
194 void Logger::shutdown() {
195     if (_enabled) {
196         while (!tag_stack().empty()) {
197             finish();
198         }
199     }
206 /*
207   Local Variables:
208   mode:c++
209   c-file-style:"stroustrup"
210   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
211   indent-tabs-mode:nil
212   fill-column:99
213   End:
214 */
215 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :