source: ogClient-Git/ogclient

Last change on this file was 30fdcce, checked in by Jose M. Guisado <jguisado@…>, 23 months ago

src: improve logging

Adds new logging handler redirecting messages to the log file
located in the Samba shared directory (applies to live mode
clients, i.e: ogLive)

Parses log level configuration from ogclient.json. See:

{

"opengnsys": {

...
"log": "INFO",
...

}
...

}

Adds --debug option to set root logger level to DEBUG when starting
ogClient. Overrides log level from config file.

In addition:

  • Replaces any occurence of print with a corresponding logging function.
  • Unsets log level for handlers, use root logger level instead.
  • Default level for root logger is INFO.
  • Replaces level from response log messages to debug (ogRest)
  • Property mode set to 100755
File size: 2.5 KB
Line 
1#!/usr/bin/python3
2
3#
4# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
5#
6# This program is free software: you can redistribute it and/or modify it under
7# the terms of the GNU Affero General Public License as published by the
8# Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10
11import json
12import logging
13import argparse
14import platform
15import subprocess
16try:
17        from signal import SIG_DFL, SIGPIPE
18except ImportError:
19        from signal import SIG_DFL
20
21
22from src.ogClient import *
23from src.log import configure_logging
24
25
26def send_event_dgram(msg, ip='127.0.0.1', port=55885):
27        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
28        sock.sendto(bytes(msg, "utf-8"), (ip, port))
29
30
31def create_parser():
32        events = ['login', 'logout']
33        parser = argparse.ArgumentParser()
34        parser.set_defaults(func=None)
35        subparsers = parser.add_subparsers()
36
37        parser_event = subparsers.add_parser('event')
38
39        subparsers_event = parser_event.add_subparsers()
40        parser_event_login = subparsers_event.add_parser('login')
41        parser_event_login.set_defaults(func=lambda x: send_event_dgram(f'session start {x.user}'))
42        parser_event_login.add_argument('user', type=str)
43        parser_event_logout = subparsers_event.add_parser('logout')
44        parser_event_logout.set_defaults(func=lambda x: send_event_dgram(f'session stop {x.user}'))
45        parser_event_logout.add_argument('user', type=str)
46
47        parser.add_argument('-c', '--config', default="",
48                            help='ogClient JSON config file path')
49        parser.add_argument('--debug', default=False,
50                            action='store_true',
51                            help='enables debug log level')
52
53        return parser
54
55
56def main():
57        parser = create_parser()
58        args = parser.parse_args(sys.argv[1:])
59        if args.func:
60                args.func(args)
61                return
62
63        if args.config:
64                config_path = args.config
65        elif platform.system().lower() == 'linux':
66                config_path = f'{ogClient.OG_PATH}ogclient/cfg/ogclient.json'
67        else:
68                config_path = './cfg/ogclient.json'
69
70        try:
71                with open(config_path, 'r') as f:
72                        CONFIG = json.load(f)
73        except:
74                print('Error: Parsing configuration file')
75                return 0
76
77        MODE = CONFIG['opengnsys']['mode']
78        URL = CONFIG['opengnsys']['url']
79        LOGLEVEL = CONFIG['opengnsys']['log']
80
81        if MODE == 'live':
82                proc = subprocess.Popen(["browser", "-qws", URL])
83        if MODE != 'windows':
84                signal.signal(SIGPIPE, SIG_DFL)
85
86        configure_logging(MODE, LOGLEVEL)
87
88        if args.debug:
89                logging.getLogger().setLevel('DEBUG')
90
91        client = ogClient(config=CONFIG)
92        client.connect()
93        client.run()
94
95if __name__ == "__main__":
96        main()
Note: See TracBrowser for help on using the repository browser.