-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_log.c
94 lines (82 loc) · 2.07 KB
/
push_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#if DEBUG
/* Includes */
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <linux/if_ether.h>
#include "push_log.h"
/* Global variables */
bool g_debug_enabled = false;
static const char *log_level_names[] =
{ "ERROR", "EVENT", "INFO" };
/* Function Definitions */
void log_message(log_levels_t level, const char *file_name, uint16_t line_number, char *message, ...)
{
if (!g_debug_enabled)
return;
va_list list;
char log_message_1[MAX_LOG_MESSAGE_SIZE] =
{ 0 };
char log_message_2[MAX_LOG_MESSAGE_SIZE] =
{ 0 };
va_start(list, message);
snprintf(log_message_1, MAX_LOG_MESSAGE_SIZE, "[%s][%s][line: %d]: %s\r\n",
(level >= DEBUG_ERROR && level <= DEBUG_INFO) ? log_level_names[level] : "",
file_name, line_number, message);
vsnprintf(log_message_2, MAX_LOG_MESSAGE_SIZE, log_message_1, list);
va_end(list);
printf("%s", log_message_2);
}
void log_packet(char *message, uint8_t *packet, int length)
{
if (!g_debug_enabled)
return;
int i = 0;
int j = 0;
int k = 0;
char temp_1[32] = { 0 };
char temp_2[32] = { 0 };
uint8_t temp_bytes[16] = { [0 ... 15] = 0 };
char debug_string[ETH_FRAME_LEN * 5] = { [0 ... (ETH_FRAME_LEN * 5) - 1] = 0 };
for (k = 0; k < length; k += 16)
{
memset(temp_bytes, 0, 16);
memcpy(temp_bytes, &packet[k], (16 > (length - k)) ? (length - k) : 16 );
for (i = 0; i < 16; i++)
{
if (i < (length - k))
{
sprintf(temp_1, "%02x ", temp_bytes[i]);
}
else
{
sprintf(temp_1, "%3s", "");
}
if(((i + 1) % 16) == 0)
{
strcat(temp_1, " ");
for (j = (i - 15); j <= i; j++)
{
sprintf(temp_2, "%c", (j < (length - k)) ?
(isprint((int) temp_bytes[j]) ? temp_bytes[j] : '.') : ' ');
strcat(temp_1, temp_2);
if(((j + 1) % 8) == 0)
{
strcat(temp_1, " ");
}
}
strcat(temp_1, "\n");
}
else if(((i + 1) % 8) == 0)
{
strcat(temp_1, " ");
}
strcat(debug_string, temp_1);
}
}
DEBUG_PRINT(DEBUG_INFO, "%s, packet length: %d \n%s", message, length, debug_string);
}
#endif