Tracer  v0.0.1
A platform independant stack trace generator
cPrinters.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2017, Daniel Mensinger
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the Daniel Mensinger nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL Daniel Mensinger BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
27 
28 #include "defines.hpp"
29 #include "tracer.h"
30 #include "tracerInternal.hpp"
31 #include <string.h>
32 
34 #define PRINTER_CASTER(Target) \
35  TR_BOOL_t localCastOK = TR_TRUE; \
36  if (!castOK) \
37  castOK = &localCastOK; \
38  \
39  TR_BOOL_t &cOK = *castOK; \
40  \
41  cOK = TR_FALSE; \
42  Target *printer = nullptr; \
43  if (p) { \
44  printer = dynamic_cast<Target *>(p->obj.get()); \
45  if (printer) { \
46  cOK = TR_TRUE; \
47  } \
48  }
49 
50 using namespace tracer;
51 using namespace std;
52 
53 extern "C" {
54 
56 void tr_Printer__genStringPreFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK) {
58 
59  if (!outStr || cOK != TR_TRUE)
60  return;
61 
62  *outStr = nullptr;
63 
64  string str = printer->genStringPreFrame(frameNum);
65  if (!str.empty()) {
66  *outStr = reinterpret_cast<char *>(malloc(sizeof(char) * (str.length() + 1)));
67  strncpy(*outStr, str.c_str(), str.length() + 1);
68  }
69 }
70 
72 void tr_Printer__genStringForFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK) {
74 
75  if (!outStr || cOK != TR_TRUE)
76  return;
77 
78  *outStr = nullptr;
79 
80  string str = printer->genStringForFrame(frameNum);
81  if (!str.empty()) {
82  *outStr = reinterpret_cast<char *>(malloc(sizeof(char) * (str.length() + 1)));
83  strncpy(*outStr, str.c_str(), str.length() + 1);
84  }
85 }
86 
88 void tr_Printer__genStringPostFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK) {
90 
91  if (!outStr || cOK != TR_TRUE)
92  return;
93 
94  *outStr = nullptr;
95 
96  string str = printer->genStringPostFrame(frameNum);
97  if (!str.empty()) {
98  *outStr = reinterpret_cast<char *>(malloc(sizeof(char) * (str.length() + 1)));
99  strncpy(*outStr, str.c_str(), str.length() + 1);
100  }
101 }
102 
104 void tr_Printer__generateString(tr_Printer_t *p, char **outStr, TR_BOOL_t *castOK) {
106 
107  if (!outStr || cOK != TR_TRUE)
108  return;
109 
110  *outStr = nullptr;
111 
112  string str = printer->generateString();
113  if (!str.empty()) {
114  *outStr = reinterpret_cast<char *>(malloc(sizeof(char) * (str.length() + 1)));
115  strncpy(*outStr, str.c_str(), str.length() + 1);
116  }
117 }
118 
120 void tr_Printer__printToFile(tr_Printer_t *p, const char *file, TR_BOOL_t append, TR_BOOL_t *castOK) {
122 
123  if (!file || cOK != TR_TRUE)
124  return;
125 
126  printer->printToFile(file, append == TR_TRUE);
127 }
128 
132 
133  if (cOK != TR_TRUE)
134  return;
135 
136  printer->printToStdOut();
137 }
138 
142 
143  if (cOK != TR_TRUE)
144  return;
145 
146  printer->printToStdErr();
147 }
148 
151 
153 
154  if (cOK != TR_TRUE)
155  return;
156 
157  printer->enableColor();
158 }
159 
163 
164  if (cOK != TR_TRUE)
165  return;
166 
167  printer->disableColor();
168 }
169 
173 
174  if (!t || cOK != TR_TRUE)
175  return;
176 
177  printer->setTrace(t->tPTR);
178 }
179 
180 
184  memset(&cfg, 0, sizeof(cfg));
185 
187 
188  if (cOK != TR_TRUE)
189  return cfg;
190 
191  auto pCFG = printer->getConfig();
192 
193  strncpy(cfg.prefix, pCFG.prefix.c_str(), 64);
194  strncpy(cfg.seper1, pCFG.seper1.c_str(), 64);
195  strncpy(cfg.seper2, pCFG.seper2.c_str(), 64);
196  strncpy(cfg.seper3, pCFG.seper3.c_str(), 64);
197  strncpy(cfg.suffix, pCFG.suffix.c_str(), 64);
198  strncpy(cfg.colorFrameNum, pCFG.colorFrameNum.c_str(), 64);
199  strncpy(cfg.colorNotFound, pCFG.colorNotFound.c_str(), 64);
200  strncpy(cfg.colorAddress, pCFG.colorAddress.c_str(), 64);
201  strncpy(cfg.colorFuncName, pCFG.colorFuncName.c_str(), 64);
202  strncpy(cfg.colorLineInfo, pCFG.colorLineInfo.c_str(), 64);
203  strncpy(cfg.colorModule, pCFG.colorModule.c_str(), 64);
204 
205  cfg.shortenFiles = pCFG.shortenFiles ? TR_TRUE : TR_FALSE;
206  cfg.shortenModules = pCFG.shortenModules ? TR_TRUE : TR_FALSE;
207  cfg.canonicalizePaths = pCFG.canonicalizePaths ? TR_TRUE : TR_FALSE;
208 
209  return cfg;
210 }
211 
215 
216  if (cOK != TR_TRUE)
217  return;
218 
220  pCFG.prefix = cfg.prefix;
221  pCFG.seper1 = cfg.seper1;
222  pCFG.seper2 = cfg.seper2;
223  pCFG.seper3 = cfg.seper3;
224  pCFG.suffix = cfg.suffix;
225  pCFG.colorFrameNum = cfg.colorFrameNum;
226  pCFG.colorNotFound = cfg.colorNotFound;
227  pCFG.colorAddress = cfg.colorAddress;
228  pCFG.colorFuncName = cfg.colorFuncName;
229  pCFG.colorLineInfo = cfg.colorLineInfo;
230  pCFG.colorModule = cfg.colorModule;
231 
232  pCFG.shortenFiles = cfg.shortenFiles == TR_TRUE;
233  pCFG.shortenModules = cfg.shortenModules == TR_TRUE;
234  pCFG.canonicalizePaths = cfg.canonicalizePaths == TR_TRUE;
235 
236  printer->setConfig(pCFG);
237 }
238 
240 void tr_Printer__setSignum(tr_Printer_t *p, int signum, TR_BOOL_t *castOK) {
242 
243  if (cOK != TR_TRUE)
244  return;
245 
246  printer->setSignum(signum);
247 }
248 
250 void tr_Printer__addSystemEntry(tr_Printer_t *p, const char *name, const char *value, TR_BOOL_t *castOK) {
252 
253  if (cOK != TR_TRUE || !name || !value)
254  return;
255 
256  printer->addSystemEntry({name, value});
257 }
258 }
std::string colorNotFound
ANSI escape sequence for the "Not Found" color.
void tr_Printer__genStringPostFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::genStringPostFrame.
Definition: cPrinters.cpp:88
void tr_Printer__printToStdErr(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::printToStdErr.
Definition: cPrinters.cpp:140
void tr_Printer__genStringForFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::genStringForFrame.
Definition: cPrinters.cpp:72
void tr_Printer__printToStdOut(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::printToStdOut.
Definition: cPrinters.cpp:130
std::string seper1
1st seperator
char colorLineInfo[64]
ANSI escape sequence for the Line information color.
Definition: tracer.h:94
std::string colorFrameNum
ANSI escape sequence for the Frame number color.
tr_DefaultPrinter_Config_t tr_Printer__getConfig(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::DefaultPrinter::getConfig.
Definition: cPrinters.cpp:182
User configuration structure.
STL namespace.
Gnerates strings from the frame structure.
char colorAddress[64]
ANSI escape sequence for the Address color.
Definition: tracer.h:92
Internal C wrapper structure.
void tr_Printer__setConfig(tr_Printer_t *p, tr_DefaultPrinter_Config_t cfg, TR_BOOL_t *castOK)
Wrapper for tracer::DefaultPrinter::setConfig.
Definition: cPrinters.cpp:213
void tr_Printer__setSignum(tr_Printer_t *p, int signum, TR_BOOL_t *castOK)
Wrapper for tracer::SystemInfoPrinter::setSignum.
Definition: cPrinters.cpp:240
char colorFuncName[64]
ANSI escape sequence for the Function Name color.
Definition: tracer.h:93
char seper2[64]
2nd seperator
Definition: tracer.h:86
std::string seper3
3rd seperator
TR_BOOL_t shortenFiles
The source file path.
Definition: tracer.h:97
void tr_Printer__generateString(tr_Printer_t *p, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::generateString.
Definition: cPrinters.cpp:104
std::string colorAddress
ANSI escape sequence for the Address color.
TR_BOOL_t canonicalizePaths
Fixes path names if they contain "/../" or are relative.
Definition: tracer.h:99
void tr_Printer__genStringPreFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::genStringPreFrame.
Definition: cPrinters.cpp:56
char prefix[64]
Perefix (prefix [functionName])
Definition: tracer.h:84
char colorNotFound[64]
ANSI escape sequence for the "Not Found" color.
Definition: tracer.h:91
Internal C wrapper structure.
bool canonicalizePaths
Fixes path names if they contain "/../" or are relative.
void tr_Printer__printToFile(tr_Printer_t *p, const char *file, TR_BOOL_t append, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::printToFile.
Definition: cPrinters.cpp:120
char colorFrameNum[64]
ANSI escape sequence for the Frame number color.
Definition: tracer.h:90
#define PRINTER_CASTER(Target)
Makes sure that the printer can be casted and sets cOK.
Definition: cPrinters.cpp:34
TR_BOOL_t shortenModules
The executable module (.so/.dll/.exe)
Definition: tracer.h:98
Basic class for all printers.
char seper1[64]
1st seperator
Definition: tracer.h:85
char suffix[64]
suffix
Definition: tracer.h:88
void tr_Printer__disableColor(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::disableColor.
Definition: cPrinters.cpp:161
enum TR_BOOL TR_BOOL_t
Define some our own boolean values.
Definition: tracer.h:58
void tr_Printer__addSystemEntry(tr_Printer_t *p, const char *name, const char *value, TR_BOOL_t *castOK)
Wrapper for tracer::SystemInfoPrinter::addSystemEntry.
Definition: cPrinters.cpp:250
void tr_Printer__enableColor(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::enableColor.
Definition: cPrinters.cpp:150
bool shortenModules
The executable module (.so/.dll/.exe)
Prints additional information before the first stack frame.
bool shortenFiles
The source file path.
char seper3[64]
3rd seperator
Definition: tracer.h:87
User configuration structure.
Definition: tracer.h:83
std::string colorFuncName
ANSI escape sequence for the Function Name color.
std::string colorLineInfo
ANSI escape sequence for the Line information color.
void tr_Printer__setTrace(tr_Printer_t *p, tr_Tracer_t *t, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::setTrace.
Definition: cPrinters.cpp:171
tracer::Tracer * tPTR
Pointer to the tracer object to use.
std::string seper2
2nd seperator
std::string prefix
Perefix (prefix [functionName])
std::string colorModule
ANSI escape sequence for the Frame number color.
char colorModule[64]
ANSI escape sequence for the Frame number color.
Definition: tracer.h:95