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
0b45d173
Commit
0b45d173
authored
8 years ago
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor log/ping, add integrate_path for path_op
parent
bf64d3a7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
73 deletions
+48
-73
Log.h
Log.h
+19
-72
ping.h
net/ping.h
+9
-1
path_op.h
win32/path_op.h
+20
-0
No files found.
Log.h
View file @
0b45d173
#pragma once
//
#ifdef WIN32
//
#ifndef _CRT_SECURE_NO_WARNINGS
//
#define _CRT_SECURE_NO_WARNINGS
//
#endif
#ifdef WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
//#include <Windows.h>
//#endif
/*
Warnning:
The singleton pattern that log implemented is not thread-safe,
you should call log::get_instance() once in your main thread,
or else it might cause multiple constructions.
*/
#include "win32.h"
#endif
// uncomment line below to
en
able OutputDebugString
// uncomment line below to
dis
able OutputDebugString
// #define NO_WINDOWS
#include <assert.h>
...
...
@@ -40,8 +34,6 @@ 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_MEMBER jlib::log* jlib::log::instance_ = nullptr;
class
log
:
public
dp
::
singleton
<
log
>
{
enum
{
max_output_size
=
1024
*
64
,
max_single_log_file_size
=
1024
*
1024
*
10
};
...
...
@@ -57,7 +49,6 @@ private:
std
::
string
line_prefix_
=
""
;
std
::
string
log_file_prefix_
=
""
;
std
::
mutex
lock_
;
//static log* instance_;
public
:
// initializers, they should be called right after get_instance
...
...
@@ -72,26 +63,10 @@ public:
void
set_log_file_prefix
(
const
std
::
string
&
prefix
)
{
log_file_prefix_
=
prefix
;
}
void
set_output_to_file
(
bool
b
=
true
)
{
log_to_file_
=
b
;
if
(
b
)
create_file_name
();
}
std
::
string
get_log_file_path
()
const
{
return
log_file_path_
;
}
public
:
//static log* get_instance()
//{
// /*
// Warnning:
// The singleton pattern that log implemented is not thread-safe,
// you should call log::get_instance() once in your main thread,
// or else it might cause multiple constructions.
// */
// if (log::instance_ == nullptr) {
// static log log_;
// log::instance_ = &log_;
// }
// return log::instance_;
//}
~
log
()
{
running_
=
false
;
...
...
@@ -181,24 +156,12 @@ public:
*
p
++
=
'\n'
;
*
p
=
'\0'
;
//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_
)
{
#if defined(WIN32) && !defined(NO_WINDOWS)
OutputDebugStringW
(
buf
);
#if defined(WIN32)
auto
msg
=
instance
->
format_msg
(
utf8
::
u16_to_mbcs
(
buf
));
#else
std
::
printf
(
msg
.
c_str
());
#endif
}
if
(
instance
->
log_to_console_
)
{
std
::
wprintf
(
L"%s"
,
buf
);
}
auto
msg
=
instance
->
format_msg
(
utf8
::
w2a
(
buf
));
#endif
instance
->
output
(
msg
);
}
}
catch
(...)
{
assert
(
0
);
...
...
@@ -306,26 +269,6 @@ protected:
};
class
log_function
{
private
:
const
char
*
func_name_
;
std
::
chrono
::
steady_clock
::
time_point
begin_
;
public
:
log_function
(
const
char
*
func_name
)
:
func_name_
(
func_name
)
{
JLOGA
(
"%s in
\n
"
,
func_name_
);
begin_
=
std
::
chrono
::
steady_clock
::
now
();
}
~
log_function
()
{
auto
diff
=
std
::
chrono
::
steady_clock
::
now
()
-
begin_
;
auto
msec
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
diff
);
JLOGA
(
"%s out, duration: %d(ms)
\n
"
,
func_name_
,
msec
.
count
());
}
};
#define LOG_FUNCTION(func_name) jlib::log_function __log_function_object__(func_name);
#define AUTO_LOG_FUNCTION LOG_FUNCTION(__FUNCTION__);
class
range_log
{
private
:
...
...
@@ -338,10 +281,10 @@ public:
begin_
=
std
::
chrono
::
steady_clock
::
now
();
}
range_log
(
const
std
::
wstring
&
msg
)
:
msg_
()
{
msg_
=
utf8
::
w2a
(
msg
);
range_log
(
const
std
::
wstring
&
msg
)
:
msg_
()
{
msg_
=
utf8
::
w2a
(
msg
);
JLOGA
((
msg_
+
" in"
).
c_str
());
begin_
=
std
::
chrono
::
steady_clock
::
now
();
begin_
=
std
::
chrono
::
steady_clock
::
now
();
}
~
range_log
()
{
...
...
@@ -351,6 +294,10 @@ public:
}
};
#define AUTO_LOG_FUNCTION jlib::range_log __log_function_object__(__FUNCTION__);
};
This diff is collapsed.
Click to expand it.
net/ping.h
View file @
0b45d173
...
...
@@ -43,6 +43,14 @@ private:
{
std
::
string
body
(
"
\"
Hello!
\"
from Asio ping."
);
if
(
max_sequence_number_
!=
0
&&
sequence_number_
>=
max_sequence_number_
)
{
quiting_
=
true
;
timer_
.
cancel
();
socket_
.
close
();
socket_
.
get_io_service
().
stop
();
return
;
}
// Create an ICMP header for an echo request.
icmp_header
echo_request
;
echo_request
.
type
(
icmp_header
::
echo_request
);
...
...
@@ -62,7 +70,7 @@ private:
// Wait up to five seconds for a reply.
num_replies_
=
0
;
timer_
.
expires_at
(
time_sent_
+
posix_time
::
seconds
(
5
));
timer_
.
expires_at
(
time_sent_
+
posix_time
::
seconds
(
3
));
timer_
.
async_wait
(
boost
::
bind
(
&
pinger
::
handle_timeout
,
this
));
}
...
...
This diff is collapsed.
Click to expand it.
win32/path_op.h
View file @
0b45d173
#
pragma
once
#include <string>
#include <algorithm>
namespace
jlib
{
...
...
@@ -18,6 +20,24 @@ inline std::string get_exe_path_a()
return
std
::
string
(
path
).
substr
(
0
,
pos
);
}
inline
std
::
wstring
integrate_path
(
const
std
::
wstring
&
path
,
wchar_t
replace_by
=
L'_'
){
static
const
wchar_t
filter
[]
=
L"
\\
/:*?
\"
<>| "
;
auto
ret
=
path
;
for
(
auto
c
:
filter
)
{
std
::
replace
(
ret
.
begin
(),
ret
.
end
(),
c
,
replace_by
);
}
return
ret
;
}
inline
std
::
string
integrate_path
(
const
std
::
string
&
path
,
char
replace_by
=
'_'
){
static
const
char
filter
[]
=
"
\\
/:*?
\"
<>| "
;
auto
ret
=
path
;
for
(
auto
c
:
filter
)
{
std
::
replace
(
ret
.
begin
(),
ret
.
end
(),
c
,
replace_by
);
}
return
ret
;
}
}
This diff is collapsed.
Click to expand it.
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