]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/jserver/osrf_chat_main.c
Performance tweak to the logging routines.
[OpenSRF.git] / src / jserver / osrf_chat_main.c
1 #include "osrf_chat.h"
2 #include "opensrf/osrfConfig.h"
3 #include <stdio.h>
4 #include "opensrf/log.h"
5 #include <syslog.h>
6
7
8 int main( int argc, char* argv[] ) {
9
10         if( argc < 3 ) {
11                 fprintf( stderr, "Usage: %s <config_file> <config_context>\n", argv[0] );
12                 exit(0);
13         }
14
15         osrfConfig* cfg = osrfConfigInit( argv[1], argv[2] );
16         if( !cfg ) {
17                 fprintf( stderr, "Unable to load configuration file %s\n", argv[1] );
18                 return -1;
19         }
20
21         init_proc_title( argc, argv );
22         set_proc_title( "ChopChop" );
23
24         char* domain            = osrfConfigGetValue(cfg, "/domain");
25         char* secret            = osrfConfigGetValue(cfg, "/secret");
26         char* sport                     = osrfConfigGetValue(cfg, "/port");
27         char* s2sport           = osrfConfigGetValue(cfg, "/s2sport");
28         char* listenaddr        = osrfConfigGetValue(cfg, "/listen_address");
29         char* llevel            = osrfConfigGetValue(cfg, "/loglevel");
30         char* lfile                     = osrfConfigGetValue(cfg, "/logfile");
31         char* facility          = osrfConfigGetValue(cfg, "/syslog");
32
33         if(!domain)
34                 fputs( "No domain specified in configuration file\n", stderr );
35         
36         if(!secret)
37                 fputs( "No secret specified in configuration file\n", stderr );
38         
39         if(!sport)
40                 fputs( "No port specified in configuration file\n", stderr );
41         
42         if(!listenaddr)
43                 fputs( "No listen_address specified in configuration file\n", stderr );
44         
45         if(!llevel)
46                 fputs( "No loglevel specified in configuration file\n", stderr );
47         
48         if(!lfile)
49                 fputs( "No logfile specified in configuration file\n", stderr );
50         
51         if(!s2sport)
52                 fputs( "No s2sport specified in configuration file\n", stderr );
53         
54         if(!(domain && secret && sport && listenaddr && llevel && lfile && s2sport)) {
55                 fprintf(stderr, "Configuration error for ChopChop - missing key ingredient\n");
56                 return -1;
57         }
58
59         int port = atoi(sport);
60         int s2port = atoi(s2sport);
61         int level = atoi(llevel);
62
63         if(!strcmp(lfile, "syslog")) {
64                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "chopchop", level );
65                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
66
67         } else {
68                 osrfLogInit( OSRF_LOG_TYPE_FILE, "chopchop", level );
69                 osrfLogSetFile( lfile );
70         }
71
72         fprintf(stderr, "Attempting to launch ChopChop with:\n"
73                         "domain: %s\nport: %s\nlisten address: %s\nlog level: %s\nlog file: %s\n",
74                         domain, sport, listenaddr, llevel, lfile );
75
76         osrfChatServer* server = osrfNewChatServer(domain, secret, s2port);
77
78         if( osrfChatServerConnect( server, port, s2port, listenaddr ) != 0 ) {
79                 osrfLogError( OSRF_LOG_MARK, "ChopChop unable to bind to port %d on %s", port, listenaddr);
80                 return -1;
81         }
82
83         daemonize();
84         osrfChatServerWait( server );
85
86         osrfChatServerFree( server );
87         osrfConfigFree(cfg);
88
89         return 0;
90
91 }
92