Browse Source

Add log module

Ilja Kartašov 3 months ago
parent
commit
c85c6cc819
2 changed files with 161 additions and 0 deletions
  1. 103
    0
      log.c
  2. 58
    0
      log.h

+ 103
- 0
log.c View File

@@ -0,0 +1,103 @@
1
+/******************************************************************************
2
+ *
3
+ *                Copyright (c) 2017-2019 by Löwenware Ltd
4
+ *             Please, refer LICENSE file for legal information
5
+ *
6
+ ******************************************************************************/
7
+
8
+/**
9
+ * @file log.c
10
+ * @author Ilja Kartašov <ik@lowenware.com>
11
+ * @brief cStuff log module source file
12
+ *
13
+ * @see https://lowenware.com/
14
+ */
15
+#include <time.h>
16
+#include "log.h"
17
+
18
+
19
+void
20
+cstuff_log_init(CStuffLog self, uint8_t level)
21
+{
22
+	self->target = stderr;
23
+	self->level = level;
24
+}
25
+
26
+
27
+void
28
+cstuff_log_release(CStuffLog self)
29
+{
30
+	if (self->target != stderr)
31
+		fclose(self->target);
32
+}
33
+
34
+
35
+CStuffRetcode
36
+cstuff_log_set_file(CStuffLog self, const char *filename)
37
+{
38
+	FILE *fp;
39
+
40
+	if (!(fp = fopen(filename, "a")))
41
+		return CSTUFF_SYSCALL_ERROR;
42
+
43
+	self->target = fp;
44
+	return CSTUFF_SUCCESS;
45
+}
46
+
47
+
48
+int
49
+cstuff_log_printf(CStuffLog self, int level, const char *format, ...)
50
+{
51
+	int result;
52
+	va_list vl;
53
+
54
+	va_start(vl, format);
55
+	result = cstuff_log_vprintf(self, level, format, vl);
56
+	va_end(vl);
57
+
58
+	return result;
59
+}
60
+
61
+
62
+int
63
+cstuff_log_vprintf(CStuffLog self, int level, const char *format, va_list vl)
64
+{
65
+	int result;
66
+
67
+	if (self->level & level) {
68
+		FILE *f = self->target;
69
+		char *label;
70
+		time_t rawtime;
71
+		char strtime[32+1];
72
+		struct tm timeinfo;
73
+
74
+		time(&rawtime);
75
+		localtime_r(&rawtime, &timeinfo);
76
+		strftime(strtime, 32, "%F %T", &timeinfo);
77
+
78
+		switch (level)
79
+		{
80
+		case CSTUFF_LOG_ERROR:
81
+			label = "!!";
82
+			break;
83
+		case CSTUFF_LOG_ALERT:
84
+			label = "~!";
85
+			break;
86
+		case CSTUFF_LOG_DEBUG:
87
+			label = "**";
88
+			break;
89
+		default:
90
+			label = "--";
91
+		}
92
+
93
+		result = fprintf(f, "%s %s ", strtime, label);
94
+		result += vfprintf(f, format, vl);
95
+		fputc('\n', f);
96
+		result++;
97
+		fflush(f);
98
+	} else {
99
+		result = 0;
100
+	}
101
+
102
+	return result;
103
+}

+ 58
- 0
log.h View File

@@ -0,0 +1,58 @@
1
+/******************************************************************************
2
+ *
3
+ *                Copyright (c) 2017-2019 by Löwenware Ltd
4
+ *             Please, refer LICENSE file for legal information
5
+ *
6
+ ******************************************************************************/
7
+
8
+/**
9
+ * @file log.h
10
+ * @author Ilja Kartašov <ik@lowenware.com>
11
+ * @brief cStuff log module header file
12
+ *
13
+ * @see https://lowenware.com/cStuff/
14
+ */
15
+
16
+#ifndef CSTUFF_LOG_H_557ADEFE_2BE8_4B9B_A894_F08FF64008EF
17
+#define CSTUFF_LOG_H_557ADEFE_2BE8_4B9B_A894_F08FF64008EF
18
+
19
+#include <stdio.h>
20
+#include <stdarg.h>
21
+#include <stdint.h>
22
+#include "retcode.h"
23
+
24
+#define CSTUFF_LOG_OFF      (0)
25
+#define CSTUFF_LOG_ERROR    (1<<0)
26
+#define CSTUFF_LOG_ALERT    (1<<1)
27
+#define CSTUFF_LOG_STATE    (1<<2)
28
+#define CSTUFF_LOG_DEBUG    (1<<3)
29
+#define CSTUFF_LOG_ALL      (0xFF)
30
+
31
+struct cstuff_log {
32
+	FILE     *target;
33
+	uint8_t   level;
34
+};
35
+
36
+typedef struct cstuff_log * CStuffLog;
37
+
38
+
39
+void
40
+cstuff_log_init(CStuffLog self, int level);
41
+
42
+
43
+void
44
+cstuff_log_release(CStuffLog self);
45
+
46
+
47
+CStuffRetcode
48
+cstuff_log_set_file(CStuffLog self, const char *filename);
49
+
50
+
51
+int
52
+cstuff_log_printf(CStuffLog self, int level, const char *format, ...);
53
+
54
+
55
+int
56
+cstuff_log_vprintf(CStuffLog self, int level, const char *format, va_list vl);
57
+
58
+#endif /* !CSTUFF_LOG_H */

Loading…
Cancel
Save