// // netlog.exe - network port data logger. // // - Connects and reads a TCP/IP port. // - Creates and writes data to log file. // - Periodic file checkpointing. // - Optional time stamping (many formats). // - Similar to comlog for serial COM ports. // // 17-Jan-2001 tvb // #include #include #include "net_lib.h" #include "log_lib.h" char Usage[] = "\n" "Usage: netlog [options] addr port [log]\n" "\n" "Network port:\n" " /addr:#.#.#.# - Set TCP/IP address\n" " /port:# - Set port number\n" "\n" "Time Stamps:\n" " /seq - Line sequence numbers\n" " /mjd - MJD (Modified Julian Date)\n" " /utc - UTC date & time\n" " /hms - UTC hours:minutes:seconds only\n" " /ms - Show milliseconds too\n" " /xl - Excel date\n" "\n" "PC High-Resolution Timers:\n" " /et - Microsecond elapsed time\n" " /ti - Microsecond time interval\n" "\n" "Other:\n" " /log:name - Specify output log file name\n" " /max:# - Maximum number of lines to write\n" " /nth:# - Take only one of every N lines\n" " /raw - No line buffering\n" " /nl - Lines terminated by only\n" " /q - Quiet mode (log without display)\n" " /help - Display this help message\n" "\n" "Examples:\n" " netlog 192.168.0.120 1298\n" " netlog /mjd /nth:1000 192.168.0.120 1298\n" " netlog /q 192.168.0.120 1298 tsc5120.dat\n" ; struct { char *Addr; short Port; long Nth; long Max; int LogType; int Raw; int Nl; char *LogPath; } Options; void GetOptions (int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "%s", Usage); exit(1); } // Parse options. while (argc > 1 && argv[1][0] == '/') { // Network configuration. if (strncmp(argv[1], "/addr:", 6) == 0) { Options.Addr = &argv[1][6]; } else if (strncmp(argv[1], "/port:", 6) == 0) { Options.Port = atoi(&argv[1][6]); } else // Log file name. if (strncmp(argv[1], "/log:", 5) == 0) { Options.LogPath = &argv[1][5]; } else // Time stamp formats. if (strcmp(argv[1], "/seq") == 0) { Options.LogType |= TS_SEQ; } else if (strcmp(argv[1], "/mjd") == 0) { Options.LogType |= TS_MJD; } else if (strcmp(argv[1], "/xl") == 0) { Options.LogType |= TS_XL; } else if (strcmp(argv[1], "/hms") == 0) { Options.LogType |= TS_HMS; } else if (strcmp(argv[1], "/utc") == 0) { Options.LogType |= TS_YMD | TS_HMS; } else if (strcmp(argv[1], "/ms") == 0) { Options.LogType |= TS_MS; // allow ms alone to be useful if ((Options.LogType & TS_HMS) == 0) { Options.LogType |= TS_YMD | TS_HMS | TS_MS; } } else if (strcmp(argv[1], "/et") == 0) { Options.LogType |= TS_ET; } else if (strcmp(argv[1], "/ti") == 0) { Options.LogType |= TS_TI; } else // Other options. if (strncmp(argv[1], "/nth:", 5) == 0) { Options.Nth = atol(&argv[1][5]); } else if (strncmp(argv[1], "/max:", 5) == 0) { Options.Max = atol(&argv[1][5]); } else if (strcmp(argv[1], "/q") == 0) { LogQuiet = 1; } else if (strcmp(argv[1], "/raw") == 0) { Options.Raw = 1; } else if (strcmp(argv[1], "/nl") == 0) { Options.Nl = 1; } else if (strcmp(argv[1], "/help") == 0) { fprintf(stderr, "%s", Usage); exit(0); } else { fprintf(stderr, "Unknown option: %s\n", argv[1]); exit(1); } argc -= 1; argv += 1; } // Check optional arguments. if (argc > 1) { Options.Addr = argv[1]; } if (argc > 2) { Options.Port = atoi(argv[2]); } if (argc > 3) { Options.LogPath = argv[3]; } if (Options.Addr == NULL || Options.Port == 0) { fprintf(stderr, "Net address and port required\n"); exit(1); } if (Options.Nth < 1) { Options.Nth = 1; } if (Options.Max < 1) { Options.Max = 0; } return; } void main (int argc, char *argv[]) { int i, j, n, end_of_line; long In, Out; GetOptions(argc, argv); LogOpen(Options.LogPath); NetOpen(Options.Addr, Options.Port); fprintf(stderr, "** Ready...\n"); i = j = n = 0; In = Out = 0; while (1) { char Buffer[50000], c; char Line[1000]; // Refill input buffer if empty. if (i == n) { n = NetRead(Buffer, sizeof Buffer); if (n <= 0) { break; } i = 0; } Line[j++] = c = Buffer[i++]; // Decide when to write time-stamped output line. end_of_line = 0; // In raw mode, write every byte. if (Options.Raw) { end_of_line = 1; } // In newline mode, write line when \n is encountered. if (Options.Nl && c == '\n') { end_of_line = 1; } // Write if output line full. if (j == sizeof Line) { end_of_line = 1; } // Write if final byte of receive packet. if (i == n && !Options.Nl) { end_of_line = 1; } if (end_of_line) { In += 1; if ((In % Options.Nth) == 0) { LogTimeStamp(Options.LogType); LogWrite(Line, j); Out += 1; if (Options.Max != 0 && Out == Options.Max) { break; } } j = 0; } } fprintf(stderr, "\n** Done (%ld lines in, %ld lines skipped, %ld lines out)\n", In, In - Out, Out); LogClose(); NetClose(); return; } // Or use a makefile... #include "net_lib.c" #include "log_lib.c"