This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
ps_to_file.c
113 lines (93 loc) · 3 KB
/
ps_to_file.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <simplehttp/simplehttp.h>
#include <pubsubclient/pubsubclient.h>
#ifdef DEBUG
#define _DEBUG(...) fprintf(stdout, __VA_ARGS__)
#else
#define _DEBUG(...) do {;} while (0)
#endif
#define VERSION "1.3"
struct output_metadata {
char *filename_format;
char current_filename[255];
char temp_filename[255];
FILE *output_file;
};
void process_message_cb(char *message, void *cbarg)
{
struct output_metadata *data;
time_t timer;
struct tm *time_struct;
_DEBUG("process_message_cb()\n");
if (message == NULL || strlen(message) < 3) {
return;
}
data = (struct output_metadata *)cbarg;
timer = time(NULL);
time_struct = gmtime(&timer);
_DEBUG("strftime format %s\n", data->filename_format);
strftime(data->temp_filename, 255, data->filename_format, time_struct);
_DEBUG("after strftime %s\n", data->temp_filename);
if (strcmp(data->temp_filename, data->current_filename) != 0) {
_DEBUG("rolling file\n");
// roll file or open file
if (data->output_file) {
_DEBUG("closing file %s\n", data->current_filename);
fclose(data->output_file);
}
_DEBUG("opening file %s\n", data->temp_filename);
strcpy(data->current_filename, data->temp_filename);
data->output_file = fopen(data->current_filename, "ab");
}
fprintf(data->output_file, "%s\n", message);
}
void error_cb(int status_code, void *cb_arg)
{
event_loopbreak();
}
int version_cb(int value)
{
fprintf(stdout, "Version: %s\n", VERSION);
return 0;
}
int main(int argc, char **argv)
{
char *pubsub_url;
char *address;
int port;
char *path;
char *filename_format = NULL;
struct output_metadata *data;
define_simplehttp_options();
option_define_bool("version", OPT_OPTIONAL, 0, NULL, version_cb, VERSION);
option_define_str("pubsub_url", OPT_REQUIRED, "http://127.0.0.1:80/sub?multipart=0", &pubsub_url, NULL, "url of pubsub to read from");
option_define_str("filename_format", OPT_REQUIRED, NULL, &filename_format, NULL, "/var/log/pubsub.%%Y-%%m-%%d_%%H.log");
if (!option_parse_command_line(argc, argv)) {
return 1;
}
data = calloc(1, sizeof(struct output_metadata));
data->filename_format = filename_format;
data->current_filename[0] = '\0';
data->temp_filename[0] = '\0';
data->output_file = NULL;
if (simplehttp_parse_url(pubsub_url, strlen(pubsub_url), &address, &port, &path)) {
pubsubclient_main(address, port, path, process_message_cb, error_cb, data);
if (data->output_file) {
fclose(data->output_file);
}
free(address);
free(path);
} else {
fprintf(stderr, "ERROR: failed to parse pubsub_url\n");
}
free(data);
free_options();
free(pubsub_url);
free(filename_format);
return 0;
}