4
4
#include <string.h>
5
5
#include <unistd.h>
6
6
#include <time.h>
7
- #include <simplehttp/pubsubclient.h>
8
7
#include <simplehttp/simplehttp.h>
8
+ #include <pubsubclient/pubsubclient.h>
9
9
10
- #define DEBUG 0
11
- #define VERSION "1.1"
10
+ #ifdef DEBUG
11
+ #define _DEBUG (...) fprintf(stdout, __VA_ARGS__)
12
+ #else
13
+ #define _DEBUG (...) do {;} while (0)
14
+ #endif
15
+
16
+ #define VERSION "1.2"
12
17
13
18
struct output_metadata {
14
19
char * filename_format ;
@@ -17,62 +22,85 @@ struct output_metadata {
17
22
FILE * output_file ;
18
23
};
19
24
20
- void
21
- process_message_cb (char * source , void * cbarg ){
22
- if (DEBUG ) fprintf (stdout , "processing message\n" );
23
- if (source == NULL || strlen (source ) < 3 ){return ;}
24
-
25
- struct output_metadata * data = (struct output_metadata * )cbarg ;
25
+ void process_message_cb (char * message , void * cbarg )
26
+ {
27
+ struct output_metadata * data ;
28
+ time_t timer ;
29
+ struct tm * time_struct ;
30
+
31
+ _DEBUG ("process_message_cb()\n" );
32
+
33
+ if (message == NULL || strlen (message ) < 3 ) {
34
+ return ;
35
+ }
36
+
37
+ data = (struct output_metadata * )cbarg ;
26
38
27
- time_t timer = time (NULL );
28
- struct tm * time_struct = gmtime (& timer );
29
- if ( DEBUG ) fprintf ( stdout , "strftime format %s\n" , data -> filename_format );
39
+ timer = time (NULL );
40
+ time_struct = gmtime (& timer );
41
+ _DEBUG ( "strftime format %s\n" , data -> filename_format );
30
42
strftime (data -> temp_filename , 255 , data -> filename_format , time_struct );
31
- if ( DEBUG ) fprintf ( stdout , "after strftime %s\n" , data -> temp_filename );
32
- if (strcmp (data -> temp_filename , data -> current_filename ) != 0 ){
33
- if ( DEBUG ) fprintf ( stdout , "rolling file\n" );
43
+ _DEBUG ( "after strftime %s\n" , data -> temp_filename );
44
+ if (strcmp (data -> temp_filename , data -> current_filename ) != 0 ) {
45
+ _DEBUG ( "rolling file\n" );
34
46
// roll file or open file
35
- if (data -> output_file ){
36
- if ( DEBUG ) fprintf ( stdout , "closing file %s\n" , data -> current_filename );
47
+ if (data -> output_file ) {
48
+ _DEBUG ( "closing file %s\n" , data -> current_filename );
37
49
fclose (data -> output_file );
38
50
}
39
- if ( DEBUG ) fprintf ( stdout , "opening file %s\n" , data -> temp_filename );
51
+ _DEBUG ( "opening file %s\n" , data -> temp_filename );
40
52
strcpy (data -> current_filename , data -> temp_filename );
41
53
data -> output_file = fopen (data -> current_filename , "ab" );
42
54
}
43
55
44
- fprintf (data -> output_file ,"%s\n" ,source );
56
+ fprintf (data -> output_file , "%s\n" , message );
45
57
}
46
58
47
- int version_cb (int value ) {
59
+ int version_cb (int value )
60
+ {
48
61
fprintf (stdout , "Version: %s\n" , VERSION );
49
62
return 0 ;
50
63
}
51
64
52
- int
53
- main (int argc , char * * argv )
65
+ int main (int argc , char * * argv )
54
66
{
55
- char * source_address = "127.0.0.1" ;
56
- int source_port = 80 ;
67
+ char * pubsub_url ;
68
+ char * address ;
69
+ int port ;
70
+ char * path ;
57
71
char * filename_format = NULL ;
72
+ struct output_metadata * data ;
58
73
59
74
define_simplehttp_options ();
60
75
option_define_bool ("version" , OPT_OPTIONAL , 0 , NULL , version_cb , VERSION );
61
- option_define_str ("source_host" , OPT_OPTIONAL , "127.0.0.1" , & source_address , NULL , NULL );
62
- option_define_int ("source_port" , OPT_OPTIONAL , 80 , & source_port , NULL , NULL );
76
+ 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" );
63
77
option_define_str ("filename_format" , OPT_REQUIRED , NULL , & filename_format , NULL , "/var/log/pubsub.%%Y-%%m-%%d_%%H.log" );
64
78
65
79
if (!option_parse_command_line (argc , argv )){
66
80
return 1 ;
67
81
}
68
82
69
- struct output_metadata * data ;
70
- data = calloc (1 ,sizeof (* data ));
83
+ data = calloc (1 , sizeof (struct output_metadata ));
71
84
data -> filename_format = filename_format ;
72
85
data -> current_filename [0 ] = '\0' ;
73
86
data -> temp_filename [0 ] = '\0' ;
74
87
data -> output_file = NULL ;
75
88
76
- return pubsub_to_pubsub_main (source_address , source_port , process_message_cb , data );
89
+ if (simplehttp_parse_url (pubsub_url , strlen (pubsub_url ), & address , & port , & path )) {
90
+ pubsub_to_pubsub_main (address , port , path , process_message_cb , NULL );
91
+
92
+ if (data -> output_file ) {
93
+ fclose (data -> output_file );
94
+ }
95
+
96
+ free (address );
97
+ free (path );
98
+ } else {
99
+ fprintf (stderr , "ERROR: failed to parse pubsub_url\n" );
100
+ }
101
+
102
+ free (data );
103
+ free_options ();
77
104
105
+ return 0 ;
78
106
}
0 commit comments