summaryrefslogtreecommitdiff
path: root/write.py
diff options
context:
space:
mode:
Diffstat (limited to 'write.py')
-rw-r--r--write.py57
1 files changed, 49 insertions, 8 deletions
diff --git a/write.py b/write.py
index d1537aa..df902db 100644
--- a/write.py
+++ b/write.py
@@ -1,10 +1,44 @@
from stdlib import *
from sys import argv
from datetime import datetime
+from configparser import ConfigParser as config
+from os import path
+
+expanduser = path.expanduser
+
+# function to get config
+if not path.isfile(f"{expanduser('~')}/.config/jlog/jlog.conf"):
+ raise FileNotFoundError(f"Config {expanduser('~')}/.config/jlog/jlog.conf not found")
+else:
+ printinfo(f"Using config file {expanduser('~')}/.config/jlog/jlog.conf")
+config = config()
+config.read(f"{expanduser('~')}/.config/jlog/jlog.conf")
+log_file = config.get('global', 'file').replace('~', expanduser('~'))
+if not path.isfile(log_file):
+ printerror(f"Log file {log_file} not found")
+ printwarning(f"Creating new log file {log_file}")
+ open(log_file, 'x').close()
+
+else:
+ printinfo(f"Using log file {log_file}")
+
+if '-sk' not in argv:
+ try:
+ if askinput("continue? (y/n)").lower() != 'y':
+ printwarning("Reseting...")
+ from os import execv
+ from sys import executable
+ execv(executable, ['python3'] + argv)
+ except AttributeError:
+ printwarning("Reseting...")
+ from os import execv
+ from sys import executable
+ execv(executable, ['python3'] + argv)
+
def convert_PRIORITY(priority) -> str:
try:
- priority = int(priority[1])
+ priority = int(priority)
if priority == 1:
priority = "DEBUG"
elif priority == 2:
@@ -18,8 +52,9 @@ def convert_PRIORITY(priority) -> str:
elif priority == 6:
priority = "FATAL"
except ValueError:
+ priority = priority.upper()
if priority not in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]:
- raise ValueError("Invalid argument")
+ raise ValueError("Invalid priority")
return priority
@@ -32,15 +67,21 @@ def log_message(priority: int, message: str):
# main function(ui)
def main():
- priority = ainputf("Priority of log messages: ")
+ global log
+ priority = askinput("Priority of log messages: ")
# check if priority is valid
- message = ainputf("Message to log: ")
+ message = askinput("Message to log: ")
- aprintf(log_message(priority, message))
+ log = log_message(priority, message)
-if __name__ == "__main__" and not len(argv) == 3:
+if __name__ == "__main__" and not len(argv) in [3, 4]:
main()
-elif len(argv) == 3:
- print(log_message(argv[1], argv[2])) \ No newline at end of file
+elif len(argv) in [3, 4]:
+ global log
+ log = log_message(argv[1], argv[2])
+
+
+printinfo(f"Logging into file {log_file}")
+open(log_file, 'a').write(log) \ No newline at end of file