Ubox
OpenWrt core utilities
syslog.h File Reference
#include <udebug.h>

Go to the source code of this file.

Data Structures

struct  log_head
 

Macros

#define LOG_LINE_SIZE   1024
 

Typedefs

typedef void(* log_list_cb) (struct log_head *h)
 

Enumerations

enum  { SOURCE_KLOG = 0 , SOURCE_SYSLOG = 1 , SOURCE_INTERNAL = 2 , SOURCE_ANY = 0xff }
 

Functions

void log_init (int log_size)
 
void log_shutdown (void)
 
struct log_headlog_list (int count, struct log_head *h)
 
int log_buffer_init (int size)
 
void log_add (char *buf, int size, int source)
 
void ubus_notify_log (struct log_head *l)
 
void log_udebug_config (struct udebug_ubus *ctx, struct blob_attr *data, bool enabled)
 

Macro Definition Documentation

◆ LOG_LINE_SIZE

#define LOG_LINE_SIZE   1024

Definition at line 17 of file syslog.h.

Typedef Documentation

◆ log_list_cb

typedef void(* log_list_cb) (struct log_head *h)

Definition at line 40 of file syslog.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
SOURCE_KLOG 
SOURCE_SYSLOG 
SOURCE_INTERNAL 
SOURCE_ANY 

Definition at line 21 of file syslog.h.

21  {
22  SOURCE_KLOG = 0,
23  SOURCE_SYSLOG = 1,
24  SOURCE_INTERNAL = 2,
25  SOURCE_ANY = 0xff,
26 };
@ SOURCE_KLOG
Definition: syslog.h:22
@ SOURCE_ANY
Definition: syslog.h:25
@ SOURCE_INTERNAL
Definition: syslog.h:24
@ SOURCE_SYSLOG
Definition: syslog.h:23

Function Documentation

◆ log_add()

void log_add ( char *  buf,
int  size,
int  source 
)

Definition at line 147 of file syslog.c.

148 {
149  regmatch_t matches[3];
150  struct log_head *next;
151  int priority = 0;
152  int ret;
153  char *c;
154 
155  /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
156  if (!log) {
157  fprintf(stderr, "%s", buf);
158  return;
159  }
160 
161  for (c = buf; *c; c++) {
162  if (*c == '\n')
163  *c = ' ';
164  }
165 
166  c = buf + size - 2;
167  while (isspace(*c)) {
168  size--;
169  c--;
170  }
171 
172  buf[size - 1] = 0;
173 
174  /* strip the priority */
175  ret = regexec(&pat_prio, buf, 3, matches, 0);
176  if (!ret) {
177  priority = atoi(&buf[matches[1].rm_so]);
178  size -= matches[2].rm_so;
179  buf += matches[2].rm_so;
180  }
181 
182  /* strip syslog timestamp */
183  if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) {
184  size -= SYSLOG_PADDING;
185  buf += SYSLOG_PADDING;
186  }
187 
189 
190  /* debug message */
191  if ((priority & LOG_FACMASK) == LOG_LOCAL7)
192  return;
193 
194  /* find new oldest entry */
195  next = log_next(newest, size);
196  if (next > newest) {
197  while ((oldest > newest) && (oldest <= next) && (oldest != log))
199  } else {
200  //fprintf(stderr, "Log wrap\n");
201  newest->size = 0;
202  next = log_next(log, size);
203  for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size))
204  ;
205  newest = log;
206  }
207 
208  /* add the log message */
209  newest->size = size;
210  newest->id = current_id++;
212  newest->source = source;
213  clock_gettime(CLOCK_REALTIME, &newest->ts);
214  strcpy(newest->data, buf);
215 
217 
218  newest = next;
219 }
void ubus_notify_log(struct log_head *l)
Definition: logd.c:208
char data[]
Definition: syslog.h:34
unsigned int size
Definition: syslog.h:29
int source
Definition: syslog.h:32
unsigned int id
Definition: syslog.h:30
struct timespec ts
Definition: syslog.h:33
int priority
Definition: syslog.h:31
static regex_t pat_prio
Definition: syslog.c:50
#define SYSLOG_PADDING
Definition: syslog.c:40
static int current_id
Definition: syslog.c:49
static struct log_head * oldest
Definition: syslog.c:48
static struct log_head * log
Definition: syslog.c:48
static struct log_head * log_next(struct log_head *h, int size)
Definition: syslog.c:88
static void log_add_udebug(int priority, char *buf, int size, int source)
Definition: syslog.c:113
static struct log_head * newest
Definition: syslog.c:48
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_buffer_init()

int log_buffer_init ( int  size)

Definition at line 328 of file syslog.c.

329 {
330  struct log_head *_log = calloc(1, size);
331 
332  if (!_log) {
333  fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size);
334  return -1;
335  }
336 
337  if (log && ((log_size + sizeof(struct log_head)) < size)) {
338  struct log_head *start = _log;
339  struct log_head *end = ((void*) _log) + size;
340  struct log_head *l;
341 
342  l = log_list(0, NULL);
343  while ((start < end) && l && l->size) {
344  memcpy(start, l, PAD(sizeof(struct log_head) + l->size));
345  start = (struct log_head *) &l->data[PAD(l->size)];
346  l = log_list(0, l);
347  }
348  free(log);
349  newest = start;
350  newest->size = 0;
351  oldest = log = _log;
352  log_end = ((void*) log) + size;
353  } else {
354  oldest = newest = log = _log;
355  log_end = ((void*) log) + size;
356  }
357  log_size = size;
358 
359  return 0;
360 }
struct log_head * log_list(int count, struct log_head *h)
Definition: syslog.c:305
static int log_size
Definition: syslog.c:47
#define PAD(x)
Definition: syslog.c:44
static struct log_head * log_end
Definition: syslog.c:48
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_init()

void log_init ( int  log_size)

Definition at line 369 of file syslog.c.

Here is the call graph for this function:

◆ log_list()

struct log_head* log_list ( int  count,
struct log_head h 
)

Definition at line 305 of file syslog.c.

306 {
307  unsigned int min = count;
308 
309  if (count)
310  min = (count < current_id) ? (current_id - count) : (0);
311  if (!h && oldest->id >= min)
312  return oldest;
313  if (!h)
314  h = oldest;
315 
316  while (h != newest) {
317  h = log_next(h, h->size);
318  if (!h->size && (h > newest))
319  h = log;
320  if (h->id >= min && (h != newest))
321  return h;
322  }
323 
324  return NULL;
325 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_shutdown()

void log_shutdown ( void  )

◆ log_udebug_config()

void log_udebug_config ( struct udebug_ubus *  ctx,
struct blob_attr *  data,
bool  enabled 
)

Definition at line 362 of file syslog.c.

364 {
365  udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
366 }
static struct udebug ud
Definition: syslog.c:52
static struct udebug_ubus_ring rings[]
Definition: syslog.c:66

◆ ubus_notify_log()

void ubus_notify_log ( struct log_head l)

Definition at line 208 of file logd.c.

209 {
210  struct client *c;
211 
212  if (list_empty(&clients) && !log_object.has_subscribers)
213  return;
214 
215  blob_buf_init(&b, 0);
216  blobmsg_add_string(&b, "msg", l->data);
217  blobmsg_add_u32(&b, "id", l->id);
218  blobmsg_add_u32(&b, "priority", l->priority);
219  blobmsg_add_u32(&b, "source", l->source);
220  blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
221 
222  if (log_object.has_subscribers)
223  ubus_notify(&conn.ctx, &log_object, "message", b.head, -1);
224 
225  list_for_each_entry(c, &clients, list)
226  ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
227 
228  blob_buf_free(&b);
229 }
static struct blob_buf b
Definition: logd.c:33
static struct ubus_auto_conn conn
Definition: logd.c:34
static struct ubus_object log_object
Definition: logd.c:200
Definition: logd.c:54
struct list_head list
Definition: logd.c:55
struct ustream_fd s
Definition: logd.c:57
Here is the caller graph for this function: