-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.c
More file actions
103 lines (95 loc) · 2.13 KB
/
Copy pathlog.c
File metadata and controls
103 lines (95 loc) · 2.13 KB
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
94
95
96
97
98
99
100
101
102
103
/*
* log module - a simple way to log messages to a file
*
* David Kotz, May 2019
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include "log.h"
/**************** flog_init ****************/
/* Initialize the logging module.
*/
void flog_init(FILE* fp)
{
flog_v(fp, "START OF LOG");
}
/**************** flog_s ****************/
/*
* log a string to the logfile, if logging is enabled.
* The string `format` can reference '%s' to incorporate `str`.
*/
void
flog_s(FILE* fp, const char* format, const char* str)
{
if (fp != NULL && format != NULL && str != NULL) {
fprintf(fp, format, str);
fputc('\n', fp);
fflush(fp);
}
}
/**************** flog_d ****************/
/*
* log an integer to the logfile, if logging is enabled.
* The string `format` can reference '%d' to incorporate `num`.
*/
void
flog_d(FILE* fp, const char* format, const int num)
{
if (fp != NULL && format != NULL) {
fprintf(fp, format, num);
fputc('\n', fp);
fflush(fp);
}
}
/**************** flog_c ****************/
/*
* log a character to the logfile, if logging is enabled.
* The string `format` can reference '%c' to incorporate `ch`.
*/
void
flog_c(FILE* fp, const char* format, const char ch)
{
if (fp != NULL && format != NULL) {
fprintf(fp, format, ch);
fputc('\n', fp);
fflush(fp);
}
}
/**************** flog_v ****************/
/*
* log a message to the logfile, if logging is enabled.
*/
void
flog_v(FILE* fp, const char* str)
{
if (fp != NULL && str != NULL) {
fputs(str, fp);
fputc('\n', fp);
fflush(fp);
}
}
/**************** flog_e ****************/
/*
* log an error to the logfile, if logging is enabled.
* Expects the global variable errno (sys/errno.h) to indicate the error,
* so this is best used immediately after a system call.
*/
void
flog_e(FILE* fp, const char* str)
{
if (fp != NULL && str != NULL) {
fprintf(fp, "%s: %s\n", str, strerror(errno));
fflush(fp);
}
}
/**************** flog_done ****************/
/*
* Done with logging. Notes this, then disables logging.
*/
void
flog_done(FILE* fp)
{
flog_v(fp, "END OF LOG");
}