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