30
30
#include < pwd.h>
31
31
#include < grp.h>
32
32
#include < fcntl.h>
33
+ #include < syslog.h>
33
34
34
35
#include < RF24.h>
35
36
#include < MyGateway.h>
@@ -55,15 +56,46 @@ static const char *serial_tty = _TTY_NAME;
55
56
static const char *devGroupName = _TTY_GROUPNAME;
56
57
57
58
int daemonizeFlag = 0 ;
59
+
60
+ void openSyslog ()
61
+ {
62
+ setlogmask (LOG_UPTO (LOG_INFO));
63
+ openlog (NULL , 0 , LOG_USER);
64
+ }
65
+
66
+ void closeSyslog ()
67
+ {
68
+ closelog ();
69
+ }
70
+
71
+ void log (int priority, const char *format, ...)
72
+ {
73
+ va_list argptr;
74
+ va_start (argptr, format);
75
+ if (daemonizeFlag == 1 ) {
76
+ vsyslog (priority, format, argptr);
77
+ } else {
78
+ vprintf (format, argptr);
79
+ }
80
+ va_end (argptr);
81
+ }
82
+
58
83
/*
59
84
* handler for SIGINT signal
60
85
*/
61
86
void handle_sigint (int sig)
62
87
{
63
- printf ( " Received SIGINT\n " );
88
+ log (LOG_INFO, " Received SIGINT\n " );
64
89
running = 0 ;
65
90
}
66
91
92
+ void handle_sigusr1 (int sig)
93
+ {
94
+ log (LOG_INFO," Received SIGUSR1\n " );
95
+ int curLogLevel = setlogmask (0 );
96
+ if (curLogLevel != LOG_DEBUG) setlogmask (LOG_UPTO (LOG_DEBUG));
97
+ else setlogmask (LOG_UPTO (LOG_INFO));
98
+ }
67
99
68
100
/*
69
101
* callback function writting data from RF24 module to the PTY
@@ -74,7 +106,7 @@ void write_msg_to_pty(char *msg)
74
106
75
107
if (msg == NULL )
76
108
{
77
- printf ( " [callback] NULL msg received!\n " );
109
+ log (LOG_WARNING, " [callback] NULL msg received!\n " );
78
110
return ;
79
111
}
80
112
@@ -154,14 +186,15 @@ int main(int argc, char **argv)
154
186
break ;
155
187
}
156
188
}
157
-
158
- printf ( " Starting PiGatewaySerial...\n " );
159
- printf ( " Protocol version - %s\n " , LIBRARY_VERSION);
189
+ openSyslog ();
190
+ log (LOG_INFO, " Starting PiGatewaySerial...\n " );
191
+ log (LOG_INFO, " Protocol version - %s\n " , LIBRARY_VERSION);
160
192
161
193
/* register the signal handler */
162
194
signal (SIGINT, handle_sigint);
163
195
signal (SIGTERM, handle_sigint);
164
-
196
+ signal (SIGUSR1, handle_sigusr1);
197
+
165
198
/* create MySensors Gateway object */
166
199
#ifdef __PI_BPLUS
167
200
gw = new MyGateway (RPI_BPLUS_GPIO_J8_15, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ, 60 );
@@ -170,7 +203,7 @@ int main(int argc, char **argv)
170
203
#endif
171
204
if (gw == NULL )
172
205
{
173
- printf ( " Could not create MyGateway! (%d) %s\n " , errno, strerror (errno));
206
+ log (LOG_ERR, " Could not create MyGateway! (%d) %s\n " , errno, strerror (errno));
174
207
status = EXIT_FAILURE;
175
208
goto cleanup;
176
209
}
@@ -179,44 +212,44 @@ int main(int argc, char **argv)
179
212
ret = openpty (&pty_master, &pty_slave, NULL , NULL , NULL );
180
213
if (ret != 0 )
181
214
{
182
- printf ( " Could not create a PTY! (%d) %s\n " , errno, strerror (errno));
215
+ log (LOG_ERR, " Could not create a PTY! (%d) %s\n " , errno, strerror (errno));
183
216
status = EXIT_FAILURE;
184
217
goto cleanup;
185
218
}
186
219
errno = 0 ;
187
220
devGrp = getgrnam (devGroupName);
188
221
if (devGrp == NULL )
189
222
{
190
- printf ( " getgrnam: %s failed. (%d) %s\n " , devGroupName, errno, strerror (errno));
223
+ log (LOG_ERR, " getgrnam: %s failed. (%d) %s\n " , devGroupName, errno, strerror (errno));
191
224
status = EXIT_FAILURE;
192
225
goto cleanup;
193
226
}
194
227
ret = chown (ttyname (pty_slave),-1 ,devGrp->gr_gid );
195
228
if (ret == -1 )
196
229
{
197
- printf ( " chown failed. (%d) %s\n " , errno, strerror (errno));
230
+ log (LOG_ERR, " chown failed. (%d) %s\n " , errno, strerror (errno));
198
231
status = EXIT_FAILURE;
199
232
goto cleanup;
200
233
}
201
234
ret = chmod (ttyname (pty_slave),ttyPermissions);
202
235
if (ret != 0 )
203
236
{
204
- printf ( " Could not change PTY permissions! (%d) %s\n " , errno, strerror (errno));
237
+ log (LOG_ERR, " Could not change PTY permissions! (%d) %s\n " , errno, strerror (errno));
205
238
status = EXIT_FAILURE;
206
239
goto cleanup;
207
240
}
208
- printf ( " Created PTY '%s'\n " , ttyname (pty_slave));
241
+ log (LOG_INFO, " Created PTY '%s'\n " , ttyname (pty_slave));
209
242
210
243
/* create a symlink with predictable name to the PTY device */
211
244
unlink (serial_tty); // remove the symlink if it already exists
212
245
ret = symlink (ttyname (pty_slave), serial_tty);
213
246
if (ret != 0 )
214
247
{
215
- printf ( " Could not create a symlink '%s' to PTY! (%d) %s\n " , serial_tty, errno, strerror (errno));
248
+ log (LOG_ERR, " Could not create a symlink '%s' to PTY! (%d) %s\n " , serial_tty, errno, strerror (errno));
216
249
status = EXIT_FAILURE;
217
250
goto cleanup;
218
251
}
219
- printf ( " Gateway tty: %s\n " , serial_tty);
252
+ log (LOG_INFO, " Gateway tty: %s\n " , serial_tty);
220
253
221
254
close (pty_slave);
222
255
configure_master_fd (pty_master);
@@ -237,7 +270,7 @@ int main(int argc, char **argv)
237
270
ret = poll (&fds, 1 , 500 );
238
271
if (ret == -1 )
239
272
{
240
- printf ( " poll() error (%d) %s\n " , errno, strerror (errno));
273
+ log (LOG_ERR, " poll() error (%d) %s\n " , errno, strerror (errno));
241
274
}
242
275
else if (ret == 0 )
243
276
{
@@ -255,7 +288,7 @@ int main(int argc, char **argv)
255
288
size = read (pty_master, buff, sizeof (buff));
256
289
if (size < 0 )
257
290
{
258
- printf ( " read error (%d) %s\n " , errno, strerror (errno));
291
+ log (LOG_ERR, " read error (%d) %s\n " , errno, strerror (errno));
259
292
continue ;
260
293
}
261
294
buff[size] = ' \0 ' ;
@@ -267,9 +300,10 @@ int main(int argc, char **argv)
267
300
268
301
269
302
cleanup:
270
- printf ( " Exiting...\n " );
303
+ log (LOG_INFO, " Exiting...\n " );
271
304
if (gw)
272
305
delete (gw);
273
306
(void ) unlink (serial_tty);
307
+ closeSyslog ();
274
308
return status;
275
309
}
0 commit comments