]> Shamusworld >> Repos - architektonas/blob - src/base/rs_debug.cpp
e6b4f4a7f2c552fe6ffae495602262e4f92767a5
[architektonas] / src / base / rs_debug.cpp
1 // rs_debug.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/28/2010  Added this text. :-)
13 //
14
15 #include "rs_debug.h"
16
17 #include <stdarg.h>
18 #include "rs_system.h"
19
20 RS_Debug * RS_Debug::uniqueInstance = NULL;
21
22 /**
23  *  Gets the one and only RS_Debug instance
24  *  (creates a new one on first call only)
25  *
26  *  @return Pointer to the single instance of this
27  * singleton class
28  */
29 RS_Debug * RS_Debug::instance()
30 {
31         if (uniqueInstance == NULL)
32         {
33                 QDateTime now = QDateTime::currentDateTime();
34                 QString nowStr;
35                 nowStr = now.toString("yyyyMMdd_hhmmss");
36                 QString fName = QString("debug_%1.log").arg(nowStr);
37
38                 uniqueInstance = new RS_Debug;
39                 //uniqueInstance->stream = fopen(fName.latin1(), "wt");
40                 uniqueInstance->stream = stderr;
41         }
42
43         return uniqueInstance;
44 }
45
46 /**
47  * Deletes the one and only RS_Debug instance.
48  */
49 void RS_Debug::deleteInstance()
50 {
51         if (uniqueInstance != NULL)
52         {
53                 fclose(uniqueInstance->stream);
54                 delete uniqueInstance;
55         }
56 }
57
58 /**
59  * Constructor for a point with default coordinates.
60  */
61 RS_Debug::RS_Debug()
62 {
63         debugLevel = D_DEBUGGING;
64 }
65
66 /**
67  * Sets the debugging level.
68  */
69 void RS_Debug::setLevel(RS_DebugLevel level)
70 {
71         debugLevel = level;
72         print("RS_DEBUG: Warnings", D_WARNING);
73         print("RS_DEBUG: Errors", D_ERROR);
74         print("RS_DEBUG: Notice", D_NOTICE);
75         print("RS_DEBUG: Informational", D_INFORMATIONAL);
76         print("RS_DEBUG: Debugging", D_DEBUGGING);
77 }
78
79 /**
80  * Gets the current debugging level.
81  */
82 RS_Debug::RS_DebugLevel RS_Debug::getLevel()
83 {
84         return debugLevel;
85 }
86
87 /**
88  * Prints the given message to stdout.
89  */
90 void RS_Debug::print(const char * format ...)
91 {
92         if (debugLevel == D_DEBUGGING)
93         {
94                 va_list ap;
95                 va_start(ap, format);
96                 vfprintf(stream, format, ap);
97                 fprintf(stream, "\n");
98                 va_end(ap);
99                 fflush(stream);
100         }
101 }
102
103 /**
104  * Prints the given message to stdout if the current debug level
105  * is lower then the given level
106  *
107  * @param level Debug level.
108  */
109 void RS_Debug::print(RS_DebugLevel level, const char * format ...)
110 {
111         if (debugLevel >= level)
112         {
113                 va_list ap;
114                 va_start(ap, format);
115                 vfprintf(stream, format, ap);
116                 fprintf(stream, "\n");
117                 va_end(ap);
118                 fflush(stream);
119         }
120 }
121
122 /**
123  * Prints a time stamp in the format yyyyMMdd_hhmmss.
124  */
125 void RS_Debug::timestamp()
126 {
127         QDateTime now = QDateTime::currentDateTime();
128         QString nowStr = now.toString("yyyyMMdd_hh:mm:ss:zzz ");
129         fprintf(stream, "%s", nowStr.toLatin1().data());
130         fprintf(stream, "\n");
131         fflush(stream);
132 }
133
134 /**
135  * Prints the unicode for every character in the given string.
136  */
137 void RS_Debug::printUnicode(const QString & text)
138 {
139         for(int i=0; i<(int)text.length(); i++)
140                 print("[%X] %c", text.at(i).unicode(), text.at(i).toLatin1());
141 }
142
143 void RS_Debug::setStream(FILE * s)
144 {
145         stream = s;
146 }