Index

lognestmonster / c5334b7

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
20901 Dec 2020 11:43c5334b7UpdateJosh Stockin11460G

Blob @ lognestmonster / tests / util.lua

text/plain3448 bytesdownload raw
1#!/usr/bin/env lua
2
3require("hexdump")
4
5-- I/O utilities
6function printf(fmt, ...)
7 io.write(string.format(fmt, ...))
8end
9
10-- simple argparse
11function arg_verify(arg, ...)
12 for i,v in pairs{...} do
13 if arg == v then return true end
14 end
15end
16
17function arg_forward(v, position, param_count)
18 forward_valid = position + param_count <= #arg
19 if not forward_valid then
20 printf("error: arg %s requires %d parameter(s)\n", v, param_count)
21 os.exit(1)
22 end
23 return true
24end
25
26-- display outputs
27function display_help()
28 printf("usage: %s %s [OPTIONS]..\n\n", arg[-1], arg[0])
29 printf("General:\n")
30 printf(" --help, -h, -?\tDisplays this help output\n")
31 printf(" --version, -v\t\tDisplays program information and copyright info\n")
32 printf("\nFlags:\n")
33 printf(" -e, --encode\t\tRun in encode mode (default)\n")
34 printf(" -d, --decode\t\tRun in decode mode\n")
35 printf(" -p\t\t\tPrettify output\n")
36 printf(" --debug\t\tPrint debug output and exit\n")
37 printf("\nEncode params:\n")
38 printf(" -x\t\t\tObject type (STATEMENT or EVENT)\n")
39 printf(" -b\t\t\tVerbosity (INFO, DEBUG, etc.)\n")
40 printf(" -p\t\t\tParent ID\n")
41 printf(" -t\t\t\tTimestamp\n")
42 printf(" -m\t\t\tMessage\n")
43 os.exit()
44end
45
46function display_short()
47 printf("usage: %s %s [OPTIONS]..\n", arg[-1], arg[0])
48 printf("Pass --help for more info\n")
49 os.exit()
50end
51
52function display_version()
53 printf("lognestmonster Lua utils Copyright (c) 2020 Joshua 'joshuas3' Stockin\n")
54 printf("<https://joshstock.in> <https://github.com/JoshuaS3/lognestmonster>\n")
55 printf("\n")
56 printf("This software's source is licensed and distributed under the terms of\n")
57 printf("the MIT License. See the attached LICENSE for more information.\n")
58 os.exit()
59end
60
61if #arg == 0 then display_short() end
62
63VERBOSITY = {
64 INFO = 0,
65 DEBUG = 1,
66 VERBOSE = 2,
67 VERYVERBOSE = 3,
68 WARNING = 4,
69 ERROR = 5
70}
71
72SETTINGS = {
73 MODE = "ENCODE",
74 ENCODE = {
75 TYPE = "STATEMENT",
76 EVENT = {
77 PARENT_ID = 0,
78 EVENT_ID = 1,
79 TAG = ""
80 },
81 STATEMENT = {
82 VERBOSITY = "INFO",
83 PARENT_ID = 0,
84 TIMESTAMP = 0,
85 MESSAGE = ""
86 }
87 },
88 DECODE = {
89 DUMP = ""
90 },
91 PRETTIFY = false,
92 DEBUG = false
93}
94
95i = 1
96while i <= #arg do
97 v = arg[i]
98 if arg_verify(v, "--help", "-h", "-?", "help", "h", "?") then
99 display_help()
100 elseif arg_verify(v, "--version", "-v") then
101 display_version()
102 elseif arg_verify(v, "-d", "--decode") then
103 SETTINGS.MODE = "DECODE"
104 elseif arg_verify(v, "-e", "--encode") then
105 SETTINGS.MODE = "ENCODE"
106 elseif arg_verify(v, "-p") then
107 SETTINGS.PRETTIFY = true
108 elseif arg_verify(v, "-x") then
109 if SETTINGS.MODE ~= "ENCODE" then
110 printf("error: -x can only be passed in ENCODE mode\n")
111 os.exit(1)
112 end
113 arg_forward(v, i, 1)
114 val = arg[i + 1]
115 if val ~= "STATEMENT" and val ~= "EVENT" then
116 printf("error: -x should be 'STATEMENT' or 'EVENT'\n")
117 os.exit(1)
118 end
119 SETTINGS.ENCODE.TYPE = val
120 i = i + 1
121 elseif arg_verify(v, "-b") then
122 if SETTINGS.MODE ~= "ENCODE" then
123 printf("error: -b can only be passed in ENCODE mode\n")
124 os.exit(1)
125 end
126 arg_forward(v, i, 1)
127 val = arg[i + 1]
128 if VERBOSITY[val] == nil then
129 printf("error: invalid verbosity (-b): %s\n", val)
130 os.exit(1)
131 end
132 SETTINGS.ENCODE.VERBOSITY = val
133 i = i + 1
134 elseif arg_verify(v, "--debug") then
135 SETTINGS.DEBUG = true
136 else
137 display_short()
138 end
139 i = i + 1
140end
141
142if SETTINGS.DEBUG then
143 printf("Encoding type: %s\n", SETTINGS.ENCODE.TYPE)
144 printf("")
145 os.exit(0)
146end
147