Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
ddbe329f
Commit
ddbe329f
authored
May 13, 2016
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor global ok
parent
3363aa77
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
104 additions
and
94 deletions
+104
-94
Log.h
Log.h
+21
-7
chrono_wrapper.h
chrono_wrapper.h
+3
-3
global.h
global.h
+4
-84
micro_getter_setter.h
micro_getter_setter.h
+53
-0
micro_singleton.h
micro_singleton.h
+23
-0
No files found.
Log.h
View file @
ddbe329f
...
...
@@ -34,7 +34,7 @@ namespace jlib
#define JLOGB(b, l) jlib::log::dump_hex(b, l)
#define JLOGASC(b, l) jlib::log::dump_ascii(b, l)
#define IMPLEMENT_CLASS_LOG_STATIC_M
A
MBER jlib::log* jlib::log::instance_ = nullptr;
#define IMPLEMENT_CLASS_LOG_STATIC_M
E
MBER jlib::log* jlib::log::instance_ = nullptr;
class
log
:
private
boost
::
noncopyable
{
...
...
@@ -157,19 +157,32 @@ public:
log
*
instance
=
log
::
get_instance
();
if
(
instance
->
log_to_file_
||
instance
->
log_to_dbg_view_
)
{
static
wchar_t
buf
[
max_output_size
],
*
p
;
wchar_t
buf
[
max_output_size
],
*
p
;
p
=
buf
;
va_list
args
;
va_start
(
args
,
format
);
p
+=
_vsnwprintf_s
(
p
,
max_output_size
-
1
,
max_output_size
-
1
,
format
,
args
);
va_end
(
args
);
while
((
p
>
buf
)
&&
iswspace
(
*
(
p
-
1
)
))
while
((
p
>
buf
)
&&
(
*
(
p
-
1
)
==
'\r'
||
*
(
p
-
1
)
==
'\n'
))
*--
p
=
'\0'
;
*
p
++
=
'\r'
;
*
p
++
=
'\n'
;
*
p
=
'\0'
;
instance
->
output
(
instance
->
format_msg
(
utf8
::
w2a
(
buf
)));
//instance->output(instance->format_msg(utf8::w2a(buf)));
std
::
lock_guard
<
std
::
mutex
>
lock
(
instance
->
lock_
);
auto
msg
=
instance
->
format_msg
(
utf8
::
w2a
(
buf
));
if
(
instance
->
log_to_file_
)
{
instance
->
output_to_log_file
(
msg
);
}
if
(
instance
->
log_to_dbg_view_
)
{
#ifdef WIN32
OutputDebugStringW
(
buf
);
#else
std
::
printf
(
msg
.
c_str
());
#endif
}
}
}
catch
(...)
{
assert
(
0
);
...
...
@@ -182,13 +195,13 @@ public:
log
*
instance
=
log
::
get_instance
();
if
(
instance
->
log_to_file_
||
instance
->
log_to_dbg_view_
)
{
static
char
buf
[
max_output_size
],
*
p
;
char
buf
[
max_output_size
],
*
p
;
p
=
buf
;
va_list
args
;
va_start
(
args
,
format
);
p
+=
_vsnprintf_s
(
p
,
max_output_size
-
1
,
max_output_size
-
1
,
format
,
args
);
va_end
(
args
);
while
((
p
>
buf
)
&&
isspace
(
*
(
p
-
1
)
))
while
((
p
>
buf
)
&&
(
*
(
p
-
1
)
==
'\r'
||
*
(
p
-
1
)
==
'\n'
))
*--
p
=
'\0'
;
*
p
++
=
'\r'
;
*
p
++
=
'\n'
;
...
...
@@ -261,7 +274,8 @@ protected:
void
output_to_dbg_view
(
const
std
::
string
&
msg
)
{
#ifdef WIN32
OutputDebugStringA
(
msg
.
c_str
());
USES_CONVERSION
;
OutputDebugStringW
(
A2W
(
msg
.
c_str
()));
#else
std
::
printf
(
msg
.
c_str
());
#endif
...
...
chrono_wrapper.h
View file @
ddbe329f
...
...
@@ -8,7 +8,7 @@
namespace
jlib
{
std
::
string
time_point_to_string
(
const
std
::
chrono
::
system_clock
::
time_point
&
tp
,
bool
with_milliseconds
=
false
)
inline
std
::
string
time_point_to_string
(
const
std
::
chrono
::
system_clock
::
time_point
&
tp
,
bool
with_milliseconds
=
false
)
{
std
::
stringstream
ss
;
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
...
...
@@ -22,7 +22,7 @@ std::string time_point_to_string(const std::chrono::system_clock::time_point& tp
return
ss
.
str
();
}
std
::
chrono
::
system_clock
::
time_point
string_to_time_point
(
const
std
::
string
&
s
)
inline
std
::
chrono
::
system_clock
::
time_point
string_to_time_point
(
const
std
::
string
&
s
)
{
std
::
tm
tm
=
{
0
};
std
::
istringstream
ss
(
s
);
...
...
@@ -30,7 +30,7 @@ std::chrono::system_clock::time_point string_to_time_point(const std::string& s)
return
std
::
chrono
::
system_clock
::
from_time_t
(
std
::
mktime
(
&
tm
));
}
std
::
string
now_to_string
(
bool
with_milliseconds
=
false
)
inline
std
::
string
now_to_string
(
bool
with_milliseconds
=
false
)
{
return
time_point_to_string
(
std
::
chrono
::
system_clock
::
now
(),
with_milliseconds
);
}
...
...
global.h
View file @
ddbe329f
#pragma once
#include <string>
#include <chrono>
#include <mutex>
#include <boost/noncopyable.hpp>
#include "utf8.h"
#include "mtverify.h"
#include "FileOper.h"
#include "log.h"
#include "chrono_wrapper.h"
#include "MyWSAError.h"
#include "observer_macro.h"
//#include "observer_macro.h"
#include "micro_getter_setter.h"
#include "micro_singleton.h"
namespace
jlib
{
#define NAMESPACE_END };
#define DECLARE_UNCOPYABLE(classname) \
classname
(
const
classname
&
)
=
delete
;
\
classname
&
operator
=
(
const
classname
&
)
=
delete
;
#define DECLARE_SINGLETON(class_name) \
private
:
\
class_name
();
\
static
class_name
*
m_pInstance
;
\
static
std
::
mutex
m_lock4Instance
;
\
public
:
\
static
class_name
*
GetInstance
()
{
\
std
::
lock_guard
<
std
::
mutex
>
lock
(
m_lock4Instance
);
\
if
(
m_pInstance
==
NULL
){
\
m_pInstance
=
new
class_name
();
\
}
\
return
m_pInstance
;
\
}
\
static
void
ReleaseObject
()
{
\
if
(
m_pInstance
)
{
delete
m_pInstance
;
m_pInstance
=
NULL
;
}
\
}
#define IMPLEMENT_SINGLETON(class_name) \
class_name
*
class_name
::
m_pInstance
=
NULL
;
\
std
::
mutex
class_name
::
m_lock4Instance
;
// getter & setter
#define DECLARE_GETTER(type, val) \
type
get
##
val
()
const
{
\
return
val
;
\
}
#define DECLARE_SETTER(type, val) \
void
set
##
val
(
const
type
&
param
)
{
\
val
=
param
;
\
}
#define DECLARE_SETTER_NONE_CONST(type, val) \
void
set
##
val
(
type
param
)
{
\
val
=
param
;
\
}
#define DECLARE_GETTER_SETTER(type, val) \
DECLARE_GETTER
(
type
,
val
)
\
DECLARE_SETTER
(
type
,
val
)
#define DECLARE_GETTER_SETTER_INT(val) \
DECLARE_GETTER
(
int
,
val
)
\
DECLARE_SETTER
(
int
,
val
)
#define DECLARE_GETTER_STRING(val) \
const
wchar_t
*
get
##
val
()
const
{
\
return
val
;
\
}
#define DECLARE_SETTER_STRING(val) \
void
set
##
val
(
const
wchar_t
*
param
)
{
\
if
(
param
)
{
\
int
len
=
wcslen
(
param
);
\
if
(
val
)
{
delete
[]
val
;
}
\
val
=
new
wchar_t
[
len
+
1
];
\
wcscpy_s
(
val
,
len
+
1
,
param
);
\
}
else
{
\
if
(
val
)
{
delete
[]
val
;
}
\
val
=
new
wchar_t
[
1
];
\
val
[
0
]
=
0
;
\
}
\
}
#define DECLARE_GETTER_SETTER_STRING(val) \
DECLARE_GETTER
(
CString
,
val
);
\
DECLARE_SETTER
(
CString
,
val
);
#define INITIALIZE_STRING(val) { val = new wchar_t[1]; val[0] = 0; }
inline
std
::
wstring
get_exe_path
()
{
wchar_t
path
[
1024
]
=
{
0
};
...
...
micro_getter_setter.h
0 → 100644
View file @
ddbe329f
#pragma once
// getter & setter
#define DECLARE_GETTER(type, val) \
type get##val() const { \
return val; \
}
#define DECLARE_SETTER(type, val) \
void set##val(const type& param) { \
val = param;\
}
#define DECLARE_SETTER_NONE_CONST(type, val) \
void set##val(type param) { \
val = param;\
}
#define DECLARE_GETTER_SETTER(type, val) \
DECLARE_GETTER(type, val) \
DECLARE_SETTER(type, val)
#define DECLARE_GETTER_SETTER_INT(val) \
DECLARE_GETTER(int, val) \
DECLARE_SETTER(int, val)
#define DECLARE_GETTER_STRING(val) \
const wchar_t* get##val() const { \
return val; \
}
#define DECLARE_SETTER_STRING(val) \
void set##val(const wchar_t* param) { \
if (param) { \
int len = wcslen(param); \
if (val) { delete[] val; } \
val = new wchar_t[len + 1]; \
wcscpy_s(val, len + 1, param); \
} else { \
if (val) { delete[] val; } \
val = new wchar_t[1]; \
val[0] = 0; \
} \
}
#define DECLARE_GETTER_SETTER_STRING(val) \
DECLARE_GETTER(CString, val); \
DECLARE_SETTER(CString, val);
#define INITIALIZE_STRING(val) { val = new wchar_t[1]; val[0] = 0; }
micro_singleton.h
0 → 100644
View file @
ddbe329f
#pragma once
#define DECLARE_SINGLETON(class_name) \
private: \
class_name(); \
static class_name* m_pInstance; \
static std::mutex m_lock4Instance; \
public: \
static class_name* GetInstance() { \
std::lock_guard<std::mutex> lock(m_lock4Instance); \
if (m_pInstance == NULL){ \
m_pInstance = new class_name(); \
} \
return m_pInstance; \
} \
static void ReleaseObject() { \
if (m_pInstance) { delete m_pInstance; m_pInstance = NULL; } \
}
#define IMPLEMENT_SINGLETON(class_name) \
class_name* class_name::m_pInstance = NULL; \
std::mutex class_name::m_lock4Instance;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment