1 | # lognestmonster Copyright (c) 2019 Joshua 'joshuas3' Stockin |
2 | # <https://github.com/JoshuaS3/lognestmonster/>. |
3 |
|
4 |
|
5 | # This file is part of lognestmonster. |
6 |
|
7 | # lognestmonster is free software: you can redistribute it and/or modify |
8 | # it under the terms of the GNU General Public License as published by |
9 | # the Free Software Foundation, either version 3 of the License, or |
10 | # (at your option) any later version. |
11 |
|
12 | # lognestmonster is distributed in the hope that it will be useful, |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | # GNU General Public License for more details. |
16 |
|
17 | # You should have received a copy of the GNU General Public License |
18 | # along with lognestmonster. If not, see <https://www.gnu.org/licenses/>. |
19 |
|
20 | import os |
21 | import sys |
22 | import time |
23 | from utils import * |
24 | from format import * |
25 | from text import * |
26 | from args import * |
27 | from parseargs import * |
28 | from read import * |
29 | from tui import * |
30 |
|
31 | def output_exit(error=None): |
32 | exitcode = 0 |
33 | output(VERSION_SHORT) |
34 | if error is not None: # print argument error is exists |
35 | output(TEXT_RED + "error: " + RESET + error) |
36 | exitcode = 1 |
37 | output(HELP_MESSAGE) |
38 | exit(exitcode) |
39 |
|
40 | def main(): |
41 | options = parseargs(sys.argv[1:]) |
42 |
|
43 | display_help = "help" in options |
44 | display_version = "version" in options |
45 | is_status = "status" in options |
46 |
|
47 | filter_errors = "errors" in options |
48 | filter_warnings = "warnings" in options |
49 | filter_info = "info" in options |
50 | filter_debug = "debug" in options |
51 | filter_verbose = "verbose" in options |
52 | filter_veryverbose = "veryverbose" in options |
53 |
|
54 | filter_after = "after" in options |
55 | filter_before = "before" in options |
56 | filter_tag = "tag" in options |
57 |
|
58 | screen_size = term_size() |
59 | clines = screen_size[0] |
60 | ccols = screen_size[1] |
61 |
|
62 | if display_help: |
63 | output(VERSION_SHORT) |
64 |
|
65 | output("usage: lognestmonster " + USAGE_MESSAGE) |
66 |
|
67 | output() |
68 |
|
69 | for paragraph in COMMAND_INFO.split("\n\n"): |
70 | output_lines(wrap(paragraph, ccols)) |
71 |
|
72 | output() |
73 |
|
74 | args = [] |
75 |
|
76 | div1 = int(ccols/3) |
77 | div2 = int(ccols/3*2) |
78 | for arg in ARGUMENT_OPTIONS: |
79 | arg_lines = [] |
80 | indicators = wrap(", ".join(ARGUMENT_OPTIONS[arg]["indicators"]), div1) |
81 | description = wrap(ARGUMENT_OPTIONS[arg]["description"], div2) |
82 | l1 = indicators[0] |
83 | z = 0 |
84 | for line in description: |
85 | try: |
86 | l1 = indicators[z] |
87 | except: |
88 | l1 = "" |
89 | z += 1 |
90 | l = columnize([(div1, l1), (div2, line)], ccols) |
91 | arg_lines.append(l) |
92 | args += arg_lines |
93 |
|
94 | output_lines(args) |
95 | output() |
96 | output(DESCRIPTION_PYTHON_VERSION) |
97 | return |
98 | elif display_version: |
99 | output(VERSION_MESSAGE) |
100 | return |
101 | elif len(sys.argv) == 1 or type(options) is str: # argument error or no args passed |
102 | if type(options) is str: # print argument error is exists |
103 | output_exit(options) |
104 | else: |
105 | output_exit() |
106 |
|
107 | positional = sys.argv[-1] |
108 | if positional is not "-" and os.path.isfile(positional) is not True and os.path.isdir(positional) is not True: |
109 | output_exit("file unknown '" + positional + "'") |
110 |
|
111 | if positional is "-": positional = "stdin" |
112 |
|
113 | if positional is "stdin": |
114 | fd = sys.stdin |
115 | else: |
116 | try: |
117 | fd = open(positional, "rb", buffering=8192) |
118 | except: |
119 | output_exit("unable to open file '" + positional + "'") |
120 |
|
121 | if not is_status: |
122 | p = TUI() |
123 | p.folder_name = positional |
124 | p.loop() |
125 | else: |
126 | r = Reader(fd) |
127 |
|
128 | filter_verbosity_levels = [] |
129 | if filter_errors: |
130 | filter_verbosity_levels.append(5) |
131 | if filter_warnings: |
132 | filter_verbosity_levels.append(4) |
133 | if filter_info: |
134 | filter_verbosity_levels.append(0) |
135 | if filter_debug: |
136 | filter_verbosity_levels.append(1) |
137 | if filter_verbose: |
138 | filter_verbosity_levels.append(2) |
139 | if filter_veryverbose: |
140 | filter_verbosity_levels.append(3) |
141 | if filter_verbosity_levels != []: |
142 | r.filter_verbosity = filter_verbosity_levels |
143 | r.filters = True |
144 | else: |
145 | r.filter_verbosity = [0, 1, 2, 3, 4, 5] |
146 |
|
147 | if filter_after: |
148 | try: |
149 | r.filter_time_start = int(options["after"][0]) |
150 | r.filters = True |
151 | except: |
152 | output_exit("expected int for flag --after, got '" + options["after"][0] + "'") |
153 | if filter_before: |
154 | try: |
155 | r.filter_time_end = int(options["before"][0]) |
156 | r.filters = True |
157 | except: |
158 | output_exit("expected int for flag --before, got '" + options["before"][0] + "'") |
159 |
|
160 | if filter_tag: |
161 | r.filter_tag = str(options["tag"][0]) |
162 | r.filters = True |
163 | if positional is not "stdin": r.size() |
164 | else: r.seekable = False |
165 | output("File scan in progress...") |
166 | s = time.time() |
167 | def update(): |
168 | if r.statement_count % 1000 == 0: |
169 | output("{0} statements | {1} events | {2} bad bytes | {3}%".format(r.statement_count, r.event_count, r.bad_bytes, round((r.position/r.file_size)*1000)/10), end="\r") |
170 | r.onupdate(update) |
171 | r.scan() |
172 | output("{0} statements | {1} events | {2} bad bytes | {3}%".format(r.statement_count, r.event_count, r.bad_bytes, round((r.position/r.file_size)*1000)/10)) |
173 | elapsed = time.time() - s |
174 | if elapsed > 1: |
175 | output("Finished in {0} seconds".format(elapsed)) |
176 |
|
177 | fd.close() |
178 |
|
179 |
|
180 | if __name__ == "__main__": |
181 | main() |
182 |
|