#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
+#include <string.h>
#include <unistd.h>
+#include "log.h"
#include "shmget.h"
#include "telemetry.h"
#include "tele2json.h"
static struct telemetry_state_t *telemetry;
-static int log_console(void) {
+struct {
+ int count;
+ int delay;
+ bool pause;
+} cfg = {
+ 0,
+ 1000,
+ true,
+};
+
+static void msleep( int ms ) {
+ while ( ms-- > 0 )
+ usleep( 999 );
+}
+
+static int log_console( int cnt, int delay ) {
char buf[4096];
+ bool forever = !cnt;
bool last_paused = telemetry->paused;
uint64_t last_timestamp = telemetry->timestamp;
- while ( 1 ) {
+ while ( forever || cnt-- > 0 ) {
if ( tele2json( buf, sizeof buf, telemetry ) > 0 )
puts( buf );
- if ( last_paused ) {
- while ( telemetry->paused )
- usleep( 100000 );
+ if ( forever || cnt > 0 ) {
+ if ( last_paused && cfg.pause ) {
+ while ( telemetry->paused )
+ usleep( 10000 );
+ }
+ else if ( last_timestamp == telemetry->timestamp && cfg.pause ) {
+ while ( last_timestamp == telemetry->timestamp )
+ msleep( delay );
+ }
+ else
+ msleep( delay );
+ last_paused = telemetry->paused;
+ last_timestamp = telemetry->timestamp;
}
- else if ( last_timestamp == telemetry->timestamp ) {
- while ( last_timestamp == telemetry->timestamp )
- sleep( 1 );
+ }
+ return 0;
+}
+
+static void usage( const char *me )
+{
+ const char *p;
+ if ( NULL != ( p = strrchr( me, '/' ) ) )
+ me = ++p;
+ fprintf( stderr, "Usage: %s [options]\n", me );
+ fprintf( stderr,
+ " Options:\n"
+ " -d N delay in ms; default: 1000\n"
+ " -h this help page\n"
+ " -n N number of cycles; default: 0 (unlimited)\n"
+ " -p ignore pause condition\n"
+ );
+}
+
+static int config( int argc, char *argv[] ) {
+ int opt;
+ const char *ostr = "+hpd:n:";
+
+ while ( -1 != ( opt = getopt( argc, argv, ostr ) ) ) {
+ switch ( opt ) {
+ case 'd':
+ cfg.delay = strtol(optarg, NULL, 10);
+ break;
+ case 'n':
+ cfg.count = strtol(optarg, NULL, 10);
+ break;
+ case 'p':
+ cfg.pause = false;
+ break;
+ case ':':
+ EPRINT( "Missing argument for option '%c'\n", optopt );
+ usage( argv[0] );
+ return -1;
+ break;
+ case '?':
+ default:
+ EPRINT( "Unrecognized option '%c'\n", optopt );
+ case 'h':
+ usage( argv[0] );
+ return -1;
+ break;
}
- else
- sleep( 1 );
- last_paused = telemetry->paused;
- last_timestamp = telemetry->timestamp;
}
return 0;
}
int main(int argc, char *argv[]) {
+ if ( 0 != config( argc, argv ) )
+ exit( EXIT_FAILURE);
if ( NULL == ( telemetry = init_shmget( TELE_SHM_KEY, sizeof *telemetry ) ) )
exit( EXIT_FAILURE);
- if ( 0 != log_console() )
+ if ( 0 != log_console( cfg.count, cfg.delay ) )
exit( EXIT_FAILURE);
exit( EXIT_SUCCESS );
- (void)argc; (void)argv;
}