OpenZWave Library 1.6.1914
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2//
3// Utils.h
4//
5// Miscellaneous helper functions
6//
7// Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
8//
9// SOFTWARE NOTICE AND LICENSE
10//
11// This file is part of OpenZWave.
12//
13// OpenZWave is free software: you can redistribute it and/or modify
14// it under the terms of the GNU Lesser General Public License as published
15// by the Free Software Foundation, either version 3 of the License,
16// or (at your option) any later version.
17//
18// OpenZWave is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
25//
26//-----------------------------------------------------------------------------
27
28#ifndef _Utils_H
29#define _Utils_H
30
31#include "platform/Mutex.h"
32#include "platform/Log.h"
33
34#include <string>
35#include <locale>
36#include <algorithm>
37#include <sstream>
38#include <vector>
39#ifndef WIN32
40#ifndef WINRT
41#ifdef DEBUG
42#include <execinfo.h>
43#include <cxxabi.h>
44#endif
45#endif
46#endif
47
48namespace OpenZWave
49{
50 namespace Internal
51 {
58 std::string ToUpper(string const& _str);
59
66 std::string ToLower(string const& _str);
67
75 void split(std::vector<std::string>& lst, const std::string& input, const std::string& separators, bool remove_empty = true);
76
82 std::string &removewhitespace(std::string &s);
83
93 std::string& ltrim(std::string& s);
94
104 std::string& rtrim(std::string& s);
105
115 std::string& trim(std::string& s);
116
117 void PrintHex(std::string prefix, uint8_t const *data, uint32 const length);
118 string PktToString(uint8 const *data, uint32 const length);
119
121 {
123 _ref(mutex)
124 {
125 //std::cout << "Locking" << std::endl;
126 _ref->Lock();
127 }
128 ;
129
131 {
132#if 0
133 if (_ref->IsSignalled())
134 std::cout << "Already Unlocked" << std::endl;
135 else
136 std::cout << "Unlocking" << std::endl;
137#endif
138 if (!_ref->IsSignalled())
139 _ref->Unlock();
140 }
141 void Unlock()
142 {
143// std::cout << "Unlocking" << std::endl;
144 _ref->Unlock();
145 }
146 private:
147 LockGuard(const LockGuard&);
148 LockGuard& operator =(LockGuard const&);
149
151 };
152
153 string ozwdirname(string);
154
155 string intToString(int x);
156
157 const char* rssi_to_string(uint8 _data);
158
159#ifndef WIN32
160#ifndef WINRT
161#ifdef DEBUG
162 class StackTraceGenerator
163 {
164 private:
165
166 // this is a pure utils class
167 // cannot be instantiated
168 //
169 StackTraceGenerator() = delete;
170 StackTraceGenerator(const StackTraceGenerator&) = delete;
171 StackTraceGenerator& operator=(const StackTraceGenerator&) = delete;
172 ~StackTraceGenerator() = delete;
173
174 public:
175
176 static std::vector<std::string> GetTrace()
177 {
178 // record stack trace upto 128 frames
179 int callstack[128] = {};
180
181 // collect stack frames
182 int frames = backtrace((void**) callstack, 5);
183
184 // get the human-readable symbols (mangled)
185 char** strs = backtrace_symbols((void**) callstack, frames);
186
187 std::vector<std::string> stackFrames;
188 stackFrames.reserve(frames);
189
190 for (int i = 2; i < frames; ++i)
191 {
192 char functionSymbol[1024] = {};
193 char moduleName[1024] = {};
194 int offset = 0;
195 char addr[48] = {};
196
197 /*
198
199 Typically this is how the backtrace looks like:
200
201 0 <app/lib-name> 0x0000000100000e98 _Z5tracev + 72
202 1 <app/lib-name> 0x00000001000015c1 _ZNK7functorclEv + 17
203 2 <app/lib-name> 0x0000000100000f71 _Z3fn0v + 17
204 3 <app/lib-name> 0x0000000100000f89 _Z3fn1v + 9
205 4 <app/lib-name> 0x0000000100000f99 _Z3fn2v + 9
206 5 <app/lib-name> 0x0000000100000fa9 _Z3fn3v + 9
207 6 <app/lib-name> 0x0000000100000fb9 _Z3fn4v + 9
208 7 <app/lib-name> 0x0000000100000fc9 _Z3fn5v + 9
209 8 <app/lib-name> 0x0000000100000fd9 _Z3fn6v + 9
210 9 <app/lib-name> 0x0000000100001018 main + 56
211 10 libdyld.dylib 0x00007fff91b647e1 start + 0
212
213 */
214
215 // split the string, take out chunks out of stack trace
216 // we are primarily interested in module, function and address
217 sscanf(strs[i], "%*s %s %s %s %*s %d",
218 moduleName, addr, functionSymbol, &offset);
219
220 int validCppName = 0;
221 // if this is a C++ library, symbol will be demangled
222 // on success function returns 0
223 //
224 char* functionName = abi::__cxa_demangle(functionSymbol,
225 NULL, 0, &validCppName);
226
227 char stackFrame[4096] = {};
228 if (validCppName == 0) // success
229 {
230 sprintf(stackFrame, "(%s)\t0x%s — %s + %d",
231 moduleName, addr, functionName, offset);
232 }
233 else
234 {
235 // in the above traceback (in comments) last entry is not
236 // from C++ binary, last frame, libdyld.dylib, is printed
237 // from here
238 sprintf(stackFrame, "(%s)\t0x%s — %s + %d",
239 moduleName, addr, functionName, offset);
240 }
241
242 if (functionName)
243 {
244 free(functionName);
245 }
246 Log::Write(LogLevel_Warning, "Stack: %s", stackFrame);
247 std::string frameStr(stackFrame);
248 stackFrames.push_back(frameStr);
249 }
250 free(strs);
251
252 return stackFrames;
253 }
254 };
255#endif
256#endif
257#endif
258 } // namespace Internal
259} // namespace OpenZWave
260
261/* keep this outside of the namespace */
262#if (defined _WINDOWS || defined WIN32 || defined _MSC_VER) && (!defined MINGW && !defined __MINGW32__ && !defined __MINGW64__)
263#include <ctime>
264struct tm *localtime_r(const time_t *_clock, struct tm *_result);
265#endif
266
267#endif
268
#define NULL
Definition: Defs.h:81
unsigned int uint32
Definition: Defs.h:91
unsigned char uint8
Definition: Defs.h:85
Implements a platform-independent mutex–for serializing access to a shared resource.
Definition: Mutex.h:45
void Unlock()
Definition: Mutex.cpp:78
virtual bool IsSignalled()
Definition: Mutex.cpp:93
bool Lock(bool const _bWait=true)
Definition: Mutex.cpp:68
std::string ToUpper(std::string const &_str)
Definition: Utils.cpp:41
string ozwdirname(string m_path)
Definition: Utils.cpp:156
void PrintHex(std::string prefix, uint8_t const *data, uint32 const length)
Definition: Utils.cpp:124
string intToString(int x)
Definition: Utils.cpp:165
const char * rssi_to_string(uint8 _data)
Definition: Utils.cpp:174
std::string ToLower(std::string const &_str)
Definition: Utils.cpp:52
std::string & trim(std::string &s)
Trim.
Definition: Utils.cpp:98
std::string & removewhitespace(std::string &s)
Definition: Utils.cpp:64
void split(std::vector< std::string > &lst, const std::string &input, const std::string &separators, bool remove_empty)
Definition: Utils.cpp:106
string PktToString(uint8 const *data, uint32 const length)
Definition: Utils.cpp:129
std::string & ltrim(std::string &s)
Left Trim.
Definition: Utils.cpp:84
std::string & rtrim(std::string &s)
Right Trim.
Definition: Utils.cpp:91
Definition: Bitfield.cpp:31
Definition: Utils.h:121
void Unlock()
Definition: Utils.h:141
~LockGuard()
Definition: Utils.h:130
LockGuard(Internal::Platform::Mutex *mutex)
Definition: Utils.h:122