design_pattern_for_c  V 1.00
dp_timelog.c
Go to the documentation of this file.
1 
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 #include <config.h>
11 #include "dp_debug.h"
12 #include "dp_mutex.h"
13 
15 
16 /*************
17  * public define
18 *************/
20 /* @{ */
21 #define DPTLOG_FAILED (-1)
22 #define DPTLOG_SUCCESS (0)
23 /* @} */
24 
26 #define DPTLOG_DELIMITER_DEFAULT (char *)" "
27 
31 typedef struct dp_timelog_data_t{
32  //private member
33  struct timespec time;
34  char * buf;
36 
38 /* @{ */
39 static inline void dp_timelog_data_store(DPTimeLog mng);
40 /* @} */
41 
45 struct dp_timelog_t{
46  //private member
47  const char *delimiter;
48  pthread_mutex_t *lock;
49 
50  unsigned long current_num;
51  unsigned long maxstoresize;
53  size_t maxloglen;
54  char * tmpbuf;
55 };
56 
57 #define DPTLOG_LOCK(mng) DPUTIL_LOCK(mng->lock)
58 #define DPTLOG_UNLOCK DPUTIL_UNLOCK;
59 
60 static void dp_timelog_show(DPTimeLog mng);
61 
62 /*************
63  * implement
64 *************/
66 static inline void dp_timelog_data_store(DPTimeLog mng) {
67  //set time
68  dp_timelog_data_t * data = &mng->log_list[mng->current_num++];
69  clock_gettime(CLOCK_REALTIME, &data->time);
70  memcpy(data->buf, mng->tmpbuf, mng->maxloglen);
71 }
72 
73 /*interface*/
74 static void dp_timelog_show(DPTimeLog mng) {
75  unsigned long i=0;
76  for(i = 0; i < mng->current_num; i ++) {
77  //show 1 data
78  fprintf(stderr, "%u.%09lu%s%s", (unsigned int)mng->log_list[i].time.tv_sec, mng->log_list[i].time.tv_nsec, mng->delimiter, mng->log_list[i].buf);
79  }
80 }
81 
82 /*************
83  * public interface API implement
84 *************/
85 DPTimeLog dp_timelog_init(const char *delimiter, size_t maxloglen, unsigned long maxstoresize, int is_threadsafe) {
86  DPTimeLog mng=NULL;
87  size_t size = sizeof(*mng);
88  void * current_p=NULL;
89  /*add buffer data size*/
90 
91  /*add dp_timelog_data_t size*/
92  size += sizeof(dp_timelog_data_t) * maxstoresize;
93 
94  /*add buffer size + tmp buffer size*/
95  size += (maxloglen + 1) * (maxstoresize + 1);
96 
97  mng=malloc(size);
98  if(!mng) {
99  DEBUG_ERRPRINT("calloc mng error:%s\n", strerror(errno));
100  return NULL;
101  }
102  memset(mng, 0, size);
103 
104  current_p = (mng+1);
105 
106  /*set delimiter*/
107  if(delimiter) {
108  mng->delimiter = delimiter;
109  } else {
111  }
112 
113  /*set lock*/
114  if(is_threadsafe) {
115  mng->lock = malloc(sizeof(pthread_mutex_t));
116  pthread_mutex_init(mng->lock, NULL);
117  }
118 
119  mng->maxstoresize = maxstoresize;
120 
121  /*set loglist*/
122  unsigned long i;
123  mng->log_list = (dp_timelog_data_t *)current_p;
124  current_p = mng->log_list + maxstoresize;
125 
126  /*set buffer in log_list*/
127  for( i = 0; i < maxstoresize; i++ ) {
128  mng->log_list[i].buf = current_p;
129  current_p = mng->log_list[i].buf + maxloglen;
130  }
131 
132  /*set tmp buffer*/
133  mng->maxloglen = maxloglen;
134  mng->tmpbuf = current_p;
135 
136  return mng;
137 }
138 
139 int dp_timelog_print(DPTimeLog mng, const char *format, ...) {
140  //fail safe
141  if(!mng || !format) {
142  return DPTLOG_FAILED;
143  }
144 
145 DPTLOG_LOCK(mng)
146  memset(mng->tmpbuf, 0, mng->maxloglen);
147  va_list arg;
148  va_start(arg, format);
149  vsnprintf(mng->tmpbuf, mng->maxloglen, format, arg);
150  va_end(arg);
152 
153  if(mng->maxstoresize <= mng->current_num) {
154  dp_timelog_show(mng);
155  mng->current_num=0;
156  }
158 
159  return DPTLOG_SUCCESS;
160 }
161 
163  //fail safe
164  if(!mng) {
165  return ;
166  }
167 
168  pthread_mutex_t *lock=NULL;
169 DPTLOG_LOCK(mng)
170  //show log
171  dp_timelog_show(mng);
172  lock=mng->lock;
173  free(mng);
175  free(lock);
176 }
177 
pthread_mutex_t * lock
delimiter
Definition: dp_timelog.c:48
int dp_timelog_print(DPTimeLog mng, const char *format,...)
Store log.
Definition: dp_timelog.c:139
static void dp_timelog_show(DPTimeLog mng)
Definition: dp_timelog.c:74
DPTimeLog dp_timelog_init(const char *delimiter, size_t maxloglen, unsigned long maxstoresize, int is_threadsafe)
Init store log.
Definition: dp_timelog.c:85
Utility mutex API for design pattern.
size_t maxloglen
log list
Definition: dp_timelog.c:53
#define DPTLOG_LOCK(mng)
Definition: dp_timelog.c:57
unsigned long maxstoresize
numnber of current stored log
Definition: dp_timelog.c:51
void dp_timelog_exit(DPTimeLog mng)
Exit stored log, and show stored log.
Definition: dp_timelog.c:162
void(* free)(EventInstance this)
Definition: event_thread.c:39
char * tmpbuf
Definition: dp_timelog.c:54
#define DPTLOG_UNLOCK
Definition: dp_timelog.c:58
#define DPTLOG_SUCCESS
Definition: dp_timelog.c:22
struct dp_timelog_data_t dp_timelog_data_t
log data with timestamp, defined like class
Definition: dp_timelog.c:31
unsigned long current_num
Definition: dp_timelog.c:50
struct timespec time
Definition: dp_timelog.c:33
#define DPTLOG_FAILED
Definition: dp_timelog.c:21
For using debug log.
dp_timelog_data_t * log_list
max size of log_list
Definition: dp_timelog.c:52
#define DPTLOG_DELIMITER_DEFAULT
default delimiter definition
Definition: dp_timelog.c:26
DPTimeLog timelog_g
Implement of dp_timelog API, defined in dp_debug.h.
Definition: dp_timelog.c:14
char * buf
timestamp
Definition: dp_timelog.c:34
#define DEBUG_ERRPRINT(...)
Definition: dp_debug.h:69
storaged log data management structure
Definition: dp_timelog.c:45
const char * delimiter
Definition: dp_timelog.c:47
static void dp_timelog_data_store(DPTimeLog mng)
for dp_timelog_data_t
Definition: dp_timelog.c:66