Skip to content

Commit 3d19eca

Browse files
author
broxigarchen
committed
Added log. Fixed some issue in emuulator
1 parent 5796501 commit 3d19eca

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

actor.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2020 Brox Chen [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
26+
#ifndef ACTORS_H_
27+
#define ACTORS_H_
28+
29+
#include <map>
30+
31+
class actor
32+
{
33+
public:
34+
actor(void) {}
35+
actor(std::string name): actorName(name) {}
36+
virtual ~actor(void) {}
37+
38+
int registerCallback(std::string& comboname, void* (*callback)(void*))
39+
{
40+
auto search = actor::cbmap.find(comboname);
41+
if(search == actor::cbmap.end())
42+
{
43+
return -1;
44+
}
45+
search->second = callback;
46+
return 0;
47+
}
48+
int registerCallback(const char* comboname, void* (*callback)(void*))
49+
{
50+
std::string name(comboname);
51+
return this->registerCallback(name, callback);
52+
}
53+
54+
void*(*getCb(std::string comboname))(void*)
55+
{
56+
auto search = actor::cbmap.find(comboname);
57+
if(search == actor::cbmap.end())
58+
{
59+
return nullptr;
60+
}
61+
return search->second;
62+
}
63+
void*(*getCb(const char* comboname))(void*)
64+
{
65+
std::string name(comboname);
66+
return this->getCb(name);
67+
}
68+
69+
virtual int init(void) = 0;
70+
71+
private:
72+
static std::map<std::string, void*(*)(void*)> cbmap;
73+
std::string actorName;
74+
};
75+
76+
77+
78+
79+
80+
#endif

log.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2020 Brox Chen [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
#define LOG_CPP
25+
#include "log.h"
26+
27+
bool usesyslog = 0;
28+
PosixHelper::sysLog logger;
29+
30+
31+
void logToConsole(int loglevel, const char* str, ...)
32+
{
33+
va_list va, vb;
34+
va_start(va, str);
35+
va_copy(vb, va);
36+
37+
int rc = std::vsnprintf(NULL, 0, str, va);
38+
if(rc <= 0)
39+
{
40+
return;
41+
}
42+
std::string out(rc + 1, '\0');
43+
va_end(va);
44+
45+
std::vsnprintf(const_cast<char*>(out.c_str()), out.size(), str, vb);
46+
va_end(vb);
47+
48+
if(loglevel == LOG_STDOUT)
49+
{
50+
std::cout << out << std::endl;
51+
} else {
52+
std::cerr << out << std::endl;
53+
}
54+
}

log.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2020 Brox Chen [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
#ifndef LOG_H_
26+
#define LOG_H_
27+
28+
#include <string.h>
29+
#include <errno.h>
30+
#include "PosixHelper.h"
31+
32+
#ifndef LOG_CPP
33+
extern PosixHelper::sysLog logger;
34+
extern bool usesyslog;
35+
#endif
36+
37+
enum logLevel
38+
{
39+
LOG_STDOUT,
40+
LOG_STDERR,
41+
};
42+
43+
void logToConsole(int loglevel, const char* str, ...);
44+
45+
#define INFO(str, ...)\
46+
{\
47+
if(usesyslog)\
48+
logger.info(str, ##__VA_ARGS__);\
49+
else\
50+
logToConsole(LOG_STDOUT, str, ##__VA_ARGS__);\
51+
}
52+
53+
#define ERROR(str, ...)\
54+
{\
55+
if(usesyslog)\
56+
logger.error(str, ##__VA_ARGS__);\
57+
else\
58+
logToConsole(LOG_STDERR, str, ##__VA_ARGS__);\
59+
}
60+
61+
#define PERROR(str)\
62+
{\
63+
if(usesyslog)\
64+
logger.error("%s: %s", strerror(errno));\
65+
else\
66+
logToConsole(LOG_STDERR, "%s: %s", str, strerror(errno));\
67+
}
68+
69+
#define ALERT(str, ...)\
70+
{\
71+
if(usesyslog)\
72+
logger.alert(str, ##__VA_ARGS__);\
73+
else\
74+
logToConsole(LOG_STDERR, str, ##__VA_ARGS__);\
75+
}
76+
77+
#define WARNING(str, ...)\
78+
{\
79+
if(usesyslog)\
80+
logger.warning(str, ##__VA_ARGS__);\
81+
else\
82+
logToConsole(LOG_STDOUT, str, ##__VA_ARGS__);\
83+
}
84+
85+
#define CRITICAL(str, ...)\
86+
{\
87+
if(usesyslog)\
88+
logger.critical(str, ##__VA_ARGS__);\
89+
else\
90+
logToConsole(LOG_STDERR, str, ##__VA_ARGS__);\
91+
}
92+
93+
#define DEBUG(str, ...)\
94+
{\
95+
if(usesyslog)\
96+
logger.debug(str, ##__VA_ARGS__);\
97+
else\
98+
logToConsole(LOG_STDOUT, str, ##__VA_ARGS__);\
99+
}
100+
101+
#endif

0 commit comments

Comments
 (0)