Newer
Older
/*
* debug.c - for debug and logging
*
* Copyright (C) 2019 IOPSYS Software Solutions AB. All rights reserved.
*
* Author: anjan.chanda@iopsys.eu
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <map1905/map2.h>
#include <map1905/maputils.h>
static int ffd, tfd;
const char *testfile_path = "/tmp/agent.test.log";
static FILE *testfile;
static FILE *outfile;
extern const char *PROG_NAME;
extern int verbose;
extern bool syslogging;
extern bool usefifo;
static const int syslog_level[] = {LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG};
void start_logging(void)
{
if (syslogging)
openlog(PROG_NAME, 0, LOG_DAEMON);
if (!outfile)
{
if (outfile_path)
{
if (usefifo)
{
struct stat st;
int rfd;
if (stat(outfile_path, &st))
unlink(outfile_path);
mkfifo(outfile_path, 0600);
if (stat(outfile_path, &st) == -1 ||
outfile = stderr;
}
}
}
void stop_logging(void)
{
if (syslogging)
closelog();
if (outfile)
fclose(outfile);
close(ffd);
unlink(outfile_path);
}
}
void log_message(int level, const char *fmt, ...)
{
va_list args;
time_t now = time(NULL);
struct tm *tm_now = localtime(&now);
const char *tm_fmt = "[%d-%02d-%02d %02d:%02d:%02d] ";
va_start(args, fmt);
dprintf(ffd, tm_fmt,
tm_now->tm_year + 1900,
tm_now->tm_mon + 1,
tm_now->tm_mday,
tm_now->tm_hour,
tm_now->tm_min,
tm_now->tm_sec);
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
vdprintf(ffd, fmt, args);
va_end(args);
}
return;
}
va_start(args, fmt);
if (syslogging && level >= 0)
vsyslog(syslog_level[level], fmt, args);
if (outfile)
vfprintf(outfile, fmt, args);
if (usefifo && ffd)
vdprintf(ffd, fmt, args);
va_end(args);
}
void dump(unsigned char *buf, int len, char *label)
{
int i;
if (label)
printf("---- %s ----", label);
if (!(i % 4))
printf(" ");
if (!(i % 16))
printf("\n ");
printf("%02x ", buf[i] & 0xff);
}
if (label)
printf("\n--------------\n");
}
// TODO: duplicate of start_logging
void start_test_logging(void)
{
if (!testfile)
{
if (testfile_path)
{
if (usefifo)
{
struct stat st;
int rfd;
if (stat(testfile_path, &st))
unlink(testfile_path);
mkfifo(testfile_path, 0600);
if (stat(testfile_path, &st) == -1 ||
testfile = fopen(testfile_path, "w+");
}
testfile = stderr;
}
}
}
void stop_test_logging(void)
{
if (testfile)
fclose(testfile);
close(tfd);
unlink(testfile_path);
}
}
void log_test(int level, void *var, int len)
{
char *bstr;
if (level > verbose)
return;
bstr = calloc(1, (len * 2) + 1);
if (!bstr)
return;
btostr(var, len, bstr);
fprintf(testfile, "%s\n", bstr);
fflush(testfile);
free(bstr);
}
struct cmdu_cstruct *cmdu = (struct cmdu_cstruct *)var;
struct json_object *msgversion, *msgtype, *msgid, *msg;
struct json_object *intfname, *tlvarray, *direction;
struct json_object *main_object;
char *bstr;
main_object = json_object_new_object();
tlvarray = json_object_new_array();
if (level > verbose)
return;
uint16_t len;
uint8_t *btlv;
struct json_object *tlv;
struct json_object *tlv_type;
struct json_object *tlv_object;
tlv_object = json_object_new_object();
uint8_t *type;
btlv = map_put_tlv_cstruct(cmdu->tlvs[i], &len);
if (!btlv)
continue;
bstr = calloc(1, (len * 2) + 1);
if (!bstr)
return;
btostr(btlv, len, bstr);
tlv = json_object_new_string(bstr);
tlv_type = json_object_new_string(map_stringify_tlv_type(*type));
json_object_object_add(tlv_object, "data", tlv);
json_object_object_add(tlv_object, "type", tlv_type);
msgversion = json_object_new_int(cmdu->message_version);
msgtype = json_object_new_int(cmdu->message_type);
direction = json_object_new_boolean(flag);
msgid = json_object_new_int(cmdu->message_id);
intfname = json_object_new_string(cmdu->intf_name);
msg = json_object_new_string(map_stringify_cmdu_type(cmdu->message_type));
json_object_object_add(main_object, "message_version", msgversion);
json_object_object_add(main_object, "message_type", msgtype);
json_object_object_add(main_object, "message_id", msgid);
json_object_object_add(main_object, "interface_name", intfname);
json_object_object_add(main_object, "message", msg);
json_object_object_add(main_object, "direction", direction);
json_object_object_add(main_object, "tlvs", tlvarray);
fprintf(testfile, "%s\n", json_object_to_json_string(main_object));
fflush(testfile);
}