Flowgrind
Advanced TCP traffic generator
fg_log.c File Reference
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include "fg_log.h"
#include "fg_time.h"
#include "fg_error.h"

Go to the source code of this file.

Functions

void close_logging (void)
 Close logging stream. More...
 
void init_logging (enum log_streams stream)
 Open logging stream. More...
 
void logging (int priority, const char *fmt,...)
 
void vlogging (int priority, const char *fmt, va_list ap)
 

Variables

static enum log_streams log_stream = LOG_SYSLOG
 

Function Documentation

◆ close_logging()

void close_logging ( void  )

Close logging stream.

Definition at line 57 of file fg_log.c.

58 {
59  switch (log_stream) {
60  case LOGGING_SYSLOG:
61  closelog();
62  break;
63  case LOGGING_STDERR:
64  case LOGGING_STDOUT:
65  break;
66  }
67 }

◆ init_logging()

void init_logging ( enum log_streams  stream)

Open logging stream.

Parameters
[in]streamto which output stream we log

Definition at line 43 of file fg_log.c.

44 {
45  log_stream = stream;
46 
47  switch (log_stream) {
48  case LOGGING_SYSLOG:
49  openlog("flowgrindd", LOG_NDELAY | LOG_CONS | LOG_PID, LOG_DAEMON);
50  break;
51  case LOGGING_STDERR:
52  case LOGGING_STDOUT:
53  break;
54  }
55 }

◆ logging()

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

Definition at line 69 of file fg_log.c.

70 {
71  va_list ap;
72 
73  va_start(ap, fmt);
74  vlogging(priority, fmt, ap);
75  va_end(ap);
76 }

◆ vlogging()

void vlogging ( int  priority,
const char *  fmt,
va_list  ap 
)

Definition at line 78 of file fg_log.c.

79 {
80  char timestamp[30] = "";
81  ctimenow_r(timestamp, sizeof(timestamp), false);
82 
83  switch (log_stream) {
84  case LOGGING_SYSLOG:
85  vsyslog(priority, fmt, ap);
86  break;
87  case LOGGING_STDERR:
88  fprintf(stderr, "%s ", timestamp);
89  vfprintf(stderr, fmt, ap);
90  fprintf(stderr, "\n");
91  fflush(stderr);
92  break;
93  case LOGGING_STDOUT:
94  fprintf(stdout, "%s ", timestamp);
95  vfprintf(stdout, fmt, ap);
96  fprintf(stdout, "\n");
97  fflush(stdout);
98  break;
99  }
100 
101 }

Variable Documentation

◆ log_stream

enum log_streams log_stream = LOG_SYSLOG
static

Definition at line 41 of file fg_log.c.

vlogging
void vlogging(int priority, const char *fmt, va_list ap)
Definition: fg_log.c:78
LOGGING_STDERR
@ LOGGING_STDERR
Log to stderr.
Definition: fg_log.h:41
LOGGING_STDOUT
@ LOGGING_STDOUT
Log to stdout.
Definition: fg_log.h:43
log_stream
static enum log_streams log_stream
Definition: fg_log.c:41
ctimenow_r
const char * ctimenow_r(char *buf, size_t size, bool ns)
Returns the current wall-clock time as null-terminated string.
Definition: fg_time.c:47
LOGGING_SYSLOG
@ LOGGING_SYSLOG
Log to syslog.
Definition: fg_log.h:39