-
Notifications
You must be signed in to change notification settings - Fork 2
/
elog_port.c
146 lines (124 loc) · 3.41 KB
/
elog_port.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015, Armink, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Function: Portable interface for linux.
* Created on: 2015-04-28
*/
#include <elog.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
static pthread_mutex_t output_lock;
// zhihengw add
static FILE* logfile=NULL;
/**
* EasyLogger port initialize
*
* @return result
*/
ElogErrCode elog_port_init(void) {
ElogErrCode result = ELOG_NO_ERR;
pthread_mutex_init(&output_lock, NULL);
//TODO(zhihengw): 创建log文件
logfile=fopen("/home/root/log.txt","a+");
if(NULL==logfile)
{
exit(-1);
}
// set buffer
setbuf(logfile,NULL);
return result;
}
// zhihengw add
ElogErrCode elog_port_close(void) {
ElogErrCode result = ELOG_NO_ERR;
// zhihengw add
fclose(logfile);
return result;
}
/**
* output log port interface
*
* @param log output of log
* @param size log size
*/
void elog_port_output(const char *log, size_t size) {
/* output to terminal */
// printf("%.*s", (int)size, log);
// output to log file added by zhihengw
fprintf(logfile,"%.*s",(int)size, log);
//fflush(logfile);
}
/**
* output lock
*/
void elog_port_output_lock(void) {
pthread_mutex_lock(&output_lock);
}
/**
* output unlock
*/
void elog_port_output_unlock(void) {
pthread_mutex_unlock(&output_lock);
}
/**
* get current time interface
*
* @return current time
*/
const char *elog_port_get_time(void) {
static char cur_system_time[24] = { 0 };
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
if (p == NULL) {
return "";
}
snprintf(cur_system_time, 18, "%02d-%02d %02d:%02d:%02d", p->tm_mon + 1, p->tm_mday,
p->tm_hour, p->tm_min, p->tm_sec);
return cur_system_time;
}
/**
* get current process name interface
*
* @return current process name
*/
const char *elog_port_get_p_info(void) {
static char cur_process_info[10] = { 0 };
snprintf(cur_process_info, 10, "pid:%04d", getpid());
return cur_process_info;
}
/**
* get current thread name interface
*
* @return current thread name
*/
const char *elog_port_get_t_info(void) {
static char cur_thread_info[10] = { 0 };
snprintf(cur_thread_info, 10, "tid:%04ld", pthread_self());
return cur_thread_info;
}