libubox
C utility functions for OpenWrt.
ulog.c File Reference
#include "ulog.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

Go to the source code of this file.

Functions

static const char * ulog_default_ident (void)
 
static void ulog_defaults (void)
 
 __attribute__ ((format(printf, 2, 0)))
 
void ulog_udebug (struct udebug_buf *_udb)
 
void ulog_open (int channels, int facility, const char *ident)
 
void ulog_close (void)
 
void ulog_threshold (int threshold)
 
void ulog (int priority, const char *fmt,...)
 

Variables

static int _ulog_channels = -1
 
static int _ulog_facility = -1
 
static int _ulog_threshold = LOG_DEBUG
 
static int _ulog_initialized = 0
 
static const char * _ulog_ident = NULL
 
static struct udebug_bufudb = NULL
 

Function Documentation

◆ __attribute__()

__attribute__ ( (format(printf, 2, 0))  )

Definition at line 91 of file ulog.c.

93 {
94  FILE *kmsg;
95 
96  if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
97  fprintf(kmsg, "<%u>", priority);
98 
99  if (_ulog_ident)
100  fprintf(kmsg, "%s: ", _ulog_ident);
101 
102  vfprintf(kmsg, fmt, ap);
103  fclose(kmsg);
104  }
105 }
FILE(GLOB test_cases "test-*.c") MACRO(ADD_FUZZER_TEST name) ADD_EXECUTABLE($
Definition: CMakeLists.txt:1
static const char * _ulog_ident
Definition: ulog.c:31
Here is the call graph for this function:

◆ ulog()

void ulog ( int  priority,
const char *  fmt,
  ... 
)

Definition at line 154 of file ulog.c.

155 {
156  va_list ap;
157 
158  if (udb) {
159  va_start(ap, fmt);
161  udebug_entry_vprintf(udb, fmt, ap);
163  va_end(ap);
164  }
165 
166  if (priority > _ulog_threshold)
167  return;
168 
169  ulog_defaults();
170 
172  {
173  va_start(ap, fmt);
174  ulog_kmsg(priority, fmt, ap);
175  va_end(ap);
176  }
177 
179  {
180  va_start(ap, fmt);
181  ulog_stdio(priority, fmt, ap);
182  va_end(ap);
183  }
184 
186  {
187  va_start(ap, fmt);
188  ulog_syslog(priority, fmt, ap);
189  va_end(ap);
190  }
191 }
void udebug_entry_add(struct udebug_buf *buf)
Definition: udebug.c:642
int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
Definition: udebug.c:607
static void udebug_entry_init(struct udebug_buf *buf)
Definition: udebug.h:151
static struct udebug_buf * udb
Definition: ulog.c:32
static int _ulog_threshold
Definition: ulog.c:29
static int _ulog_channels
Definition: ulog.c:27
static void ulog_defaults(void)
Definition: ulog.c:55
@ ULOG_SYSLOG
Definition: ulog.h:28
@ ULOG_STDIO
Definition: ulog.h:29
@ ULOG_KMSG
Definition: ulog.h:27
Here is the call graph for this function:

◆ ulog_close()

void ulog_close ( void  )

Definition at line 138 of file ulog.c.

139 {
140  if (!_ulog_initialized)
141  return;
142 
144  closelog();
145 
146  _ulog_initialized = 0;
147 }
static int _ulog_initialized
Definition: ulog.c:30
Here is the caller graph for this function:

◆ ulog_default_ident()

static const char* ulog_default_ident ( void  )
static

Definition at line 34 of file ulog.c.

35 {
36  FILE *self;
37  static char line[64];
38  char *p = NULL;
39  char *sbuf;
40 
41  if ((self = fopen("/proc/self/status", "r")) != NULL) {
42  while (fgets(line, sizeof(line), self)) {
43  if (!strncmp(line, "Name:", 5)) {
44  strtok_r(line, "\t\n", &sbuf);
45  p = strtok_r(NULL, "\t\n", &sbuf);
46  break;
47  }
48  }
49  fclose(self);
50  }
51 
52  return p;
53 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ulog_defaults()

static void ulog_defaults ( void  )
static

Definition at line 55 of file ulog.c.

56 {
57  char *env;
58 
60  return;
61 
62  env = getenv("PREINIT");
63 
64  if (_ulog_channels < 0) {
65  if (env && !strcmp(env, "1"))
67  else if (isatty(1))
69  else
71  }
72 
73  if (_ulog_facility < 0) {
74  if (env && !strcmp(env, "1"))
75  _ulog_facility = LOG_DAEMON;
76  else if (isatty(1))
77  _ulog_facility = LOG_USER;
78  else
79  _ulog_facility = LOG_DAEMON;
80  }
81 
82  if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
84 
86  openlog(_ulog_ident, 0, _ulog_facility);
87 
89 }
static const char * ulog_default_ident(void)
Definition: ulog.c:34
static int _ulog_facility
Definition: ulog.c:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ulog_open()

void ulog_open ( int  channels,
int  facility,
const char *  ident 
)

Definition at line 129 of file ulog.c.

130 {
131  ulog_close();
132 
133  _ulog_channels = channels;
134  _ulog_facility = facility;
135  _ulog_ident = ident;
136 }
void ulog_close(void)
Definition: ulog.c:138
Here is the call graph for this function:

◆ ulog_threshold()

void ulog_threshold ( int  threshold)

Definition at line 149 of file ulog.c.

150 {
151  _ulog_threshold = threshold;
152 }

◆ ulog_udebug()

void ulog_udebug ( struct udebug_buf _udb)

Definition at line 124 of file ulog.c.

125 {
126  udb = _udb;
127 }

Variable Documentation

◆ _ulog_channels

int _ulog_channels = -1
static

Definition at line 27 of file ulog.c.

◆ _ulog_facility

int _ulog_facility = -1
static

Definition at line 28 of file ulog.c.

◆ _ulog_ident

const char* _ulog_ident = NULL
static

Definition at line 31 of file ulog.c.

◆ _ulog_initialized

int _ulog_initialized = 0
static

Definition at line 30 of file ulog.c.

◆ _ulog_threshold

int _ulog_threshold = LOG_DEBUG
static

Definition at line 29 of file ulog.c.

◆ udb

struct udebug_buf* udb = NULL
static

Definition at line 32 of file ulog.c.