Skip to content

Commit 014f8d9

Browse files
committed
Updated README and fixed convertVoidToULong
1 parent ea39881 commit 014f8d9

File tree

4 files changed

+261
-12
lines changed

4 files changed

+261
-12
lines changed

README.md

+245-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,164 @@ Or include files:
1717
* `src/wsjcpp_core.h`
1818
* `src/wsjcpp_core.cpp`
1919

20-
## List of static function:
20+
## Logger (WSJCppLog)
2121

22-
### createUUID
22+
* Output will be collored for console, but for file color will be missing.
23+
* Functions is safe thread.
24+
* Logger have a log-rotate (every 51000 seconds / every day)
25+
* WSJCppLog::throw_err - will be generate `throw std::runtime_error(sMessage);`
26+
* std::vector<std::string> WSJCppLog::getLastLogMessages() - last 50 records from log
2327

24-
generation uuid but before you need call once `WSJCppCore::initRandom();` (for example in main function)
28+
In main your need init logger first.
2529

2630
```
27-
std::string sUuid = WSJCppCore::createUuid();
31+
#include <wsjcpp_core.h>
32+
33+
int main(int argc, char* argv[]) {
34+
std::string TAG = "MAIN";
35+
if (!WSJCppCore::dirExists(".logs")) {
36+
WSJCppCore::makeDir(".logs");
37+
}
38+
WSJCppLog::setLogDirectory(".logs");
39+
WSJCppLog::setPrefixLogFile("app");
40+
41+
// ...
42+
return 0;
43+
}
44+
```
45+
46+
And then in any place in code you can call static functions:
47+
48+
```
49+
#include <wsjcpp_core.h>
50+
51+
...
52+
const std::string TAG = "MAIN";
53+
WSJCppLog::info(TAG, "Hello info");
54+
WSJCppLog::err(TAG, "Hello err");
55+
WSJCppLog::warn(TAG, "Hello warn");
56+
WSJCppLog::ok(TAG, "Hello ok");
57+
```
58+
59+
Example output
60+
```
61+
2020-02-25 16:56:07.373, 0x0x10ac51dc0 [INFO] MAIN: Hello info
62+
2020-02-25 16:56:07.374, 0x0x10ac51dc0 [ERR] MAIN: Hello err
63+
2020-02-25 16:56:07.375, 0x0x10ac51dc0 [WARN] MAIN: Hello warn
64+
2020-02-25 16:56:07.376, 0x0x10ac51dc0 [OK] MAIN: Hello ok
65+
```
66+
67+
## List of static function (WSJCppCore):
68+
69+
### doNormalizePath
70+
71+
Normalize paths. for example: ".//../bin/some/../" -> "./../bin/"
72+
73+
```
74+
std::string sPath = WSJCppCore::doNormalizePath(".//../bin/some/../");
75+
```
76+
77+
### extractFilename
78+
79+
Extract base filename from fullpath.
80+
81+
```
82+
std::string sFilename = WSJCppCore::doNormalizePath(".//../bin/some/../file.txt");
83+
```
84+
85+
### getCurrentDirectory
86+
87+
```
88+
static std::string getCurrentDirectory();
89+
```
90+
91+
### currentTime_milliseconds
92+
93+
```
94+
static long currentTime_milliseconds();
95+
```
96+
97+
### currentTime_seconds
98+
99+
```
100+
static long currentTime_seconds();
101+
```
102+
103+
### currentTime_forFilename
104+
105+
```
106+
static std::string currentTime_forFilename();
107+
```
108+
109+
### currentTime_logformat
110+
111+
```
112+
static std::string currentTime_logformat();
113+
```
114+
115+
### threadId
116+
117+
```
118+
static std::string threadId();
119+
```
120+
121+
### formatTimeForWeb
122+
123+
```
124+
static std::string formatTimeForWeb(long nTimeInSec);
125+
```
126+
127+
### formatTimeForFilename
128+
129+
```
130+
static std::string formatTimeForFilename(long nTimeInSec);
131+
```
132+
133+
### formatTimeUTC
134+
135+
```
136+
static std::string formatTimeUTC(int nTimeInSec);
137+
```
138+
139+
### dirExists
140+
141+
```
142+
static bool dirExists(const std::string &sFilename);
143+
```
144+
145+
### fileExists
146+
147+
```
148+
static bool fileExists(const std::string &sFilename);
149+
```
150+
151+
### listOfDirs
152+
153+
```
154+
static std::vector<std::string> listOfDirs(const std::string &sDirname);
155+
```
156+
157+
### listOfFiles
158+
159+
```
160+
static std::vector<std::string> listOfFiles(const std::string &sDirname);
161+
```
162+
163+
### makeDir
164+
165+
Create a new directory
166+
```
167+
std::string sDirname = ".logs";
168+
if (WSJCppCore::makeDir(sDirname)) {
169+
std::cout << " Created '" << sDirname << "'" << std::endl;
170+
}
171+
```
172+
173+
### writeFile
174+
175+
```
176+
static bool writeFile(const std::string &sFilename, const std::string &sContent);
177+
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
28178
```
29179

30180
### readTextFile
@@ -35,4 +185,95 @@ std::string sContent;
35185
if (WSJCppCore::readTextFile("./file.txt", sContent)) {
36186
std::cout << sContent;
37187
}
188+
```
189+
190+
### removeFile
191+
192+
```
193+
if (WSJCppCore::removeFile("./file.txt")) {
194+
std::cout << "File removed" << std::endl;
195+
}
196+
```
197+
198+
### trim
199+
200+
```
201+
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
202+
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
203+
static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
204+
```
205+
206+
### to_lower
207+
208+
```
209+
static std::string& to_lower(std::string& str);
210+
```
211+
212+
### toUpper
213+
214+
```
215+
static std::string toUpper(const std::string& str);
216+
217+
### createUuid
218+
219+
Generation uuid but before you need call once `WSJCppCore::initRandom();` (for example in main function)
220+
221+
```
222+
WSJCppCore::initRandom(); // once in main on start
223+
std::string sUuid = WSJCppCore::createUuid();
224+
std::cout << sUuid << std::endl;
225+
```
226+
227+
Example output:
228+
229+
```
230+
b49d92ae-f11c-f8bc-3a94-e7519e341927
231+
```
232+
233+
### convertVoidToULong
234+
235+
```
236+
std::string s = "dddd";
237+
unsigned long nPointer = WSJCppCore::convertVoidToULong((void *)&s);
238+
std::cout << sPointerHex << std::endl;
239+
static unsigned long convertVoidToULong(void *p);
240+
```
241+
242+
Example output:
243+
```
244+
140732802287936
245+
```
246+
247+
### getPointerAsHex
248+
249+
```
250+
std::string s = "dddd";
251+
std::string sPointerHex = WSJCppCore::getPointerAsHex((void *)&s);
252+
std::cout << sPointerHex << std::endl;
253+
```
254+
255+
Example output:
256+
```
257+
0x7ffee8b04940
258+
```
259+
260+
### extractURLProtocol
261+
262+
```
263+
std::string sProtocol = WSJCppCore::extractURLProtocol("https://github.com/wsjcpp");
264+
```
265+
266+
### getEnv
267+
268+
Get system environments
269+
```
270+
std::string sValue;
271+
if (WSJCppCore::getEnv("PATH", sValue)) {
272+
std::cout << sValue << std::endl;
273+
}
274+
```
275+
276+
Example output:
277+
```
278+
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
38279
```

src/main.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
#include "wsjcpp_core.h"
55

66
int main(int argc, char* argv[]) {
7-
std::string TAG = "MAIN";
7+
const std::string TAG = "MAIN";
88
std::string appName = std::string(WSJCPP_NAME);
99
std::string appVersion = std::string(WSJCPP_VERSION);
10+
11+
if (!WSJCppCore::dirExists(".logs")) {
12+
WSJCppCore::makeDir(".logs");
13+
}
14+
WSJCppLog::setLogDirectory(".logs");
1015
WSJCppLog::setPrefixLogFile("wsjcpp_core");
11-
WSJCppLog::info(TAG, "Hello");
16+
17+
WSJCppLog::info(TAG, "Hello info");
18+
WSJCppLog::err(TAG, "Hello err");
19+
WSJCppLog::warn(TAG, "Hello warn");
20+
WSJCppLog::ok(TAG, "Hello ok");
1221

1322
WSJCppCore::init(
1423
argc, argv,
@@ -17,6 +26,5 @@ int main(int argc, char* argv[]) {
1726
"Evgenii Sopov",
1827
""
1928
);
20-
2129
return 0;
2230
}

src/wsjcpp_core.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ std::string WSJCppCore::createUuid() {
422422
// ---------------------------------------------------------------------
423423

424424
unsigned long WSJCppCore::convertVoidToULong(void *p) {
425-
unsigned long ret = *(unsigned long *)p;
426-
return ret;
425+
std::uintptr_t ret = reinterpret_cast<std::uintptr_t>(p);
426+
return (unsigned long)ret;
427427
}
428428

429429
// ---------------------------------------------------------------------

unit-tests.wsjcpp/src/unit_test_create_uuid.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ bool UnitTestCreateUuid::run() {
2121
std::vector<std::string> vUuids;
2222
for (int i = 0; i < 100; i++) {
2323
std::string sUuid = WSJCppCore::createUuid();
24-
/*if (i < 3) {
25-
Log::info(TAG, "sUuid: " + sUuid);
26-
}*/
24+
// if (i < 3) {
25+
// WSJCppLog::info(TAG, "sUuid: " + sUuid);
26+
// }
2727
vUuids.push_back(sUuid);
2828
}
2929
for (int x = 0; x < 100; x++) {

0 commit comments

Comments
 (0)