Commit 5c5d8e1c authored by Saswat's avatar Saswat

add logger

parent 513726a9
......@@ -40,10 +40,10 @@ $(VENV)/bin/activate: requirements.txt
run: daemon gui
daemon: $(DAEMON) $(UTILS)
$(PYTHON) src/trackingdaemon.py -d -i 10 &> log/deamon.log &
$(PYTHON) src/trackingdaemon.py -d -i $(INTERVAL)
gui: $(GUI) $(UTILS)
$(PYTHON) src/gui.py -d &> deamon.log
$(PYTHON) src/gui.py -d
pkg: dist/ActivityTracker dist/ActivityTrackerd $(PKGS_UTILS)
@echo Building Package
......
......@@ -30,6 +30,7 @@ make daemon interval=10
Data is dumped to the databse only after an event is detected after the interval of 10 sec.
Logs about the data being dumped into the database can be seen in `log/daemon.log`.
### Run GUI
In a duplicate terminal we will open the GUI.
The GUI utility to visualise the activity data can also be started using makefile.
```bash
make gui
......
import sqlite3
from datetime import datetime, timedelta
import sys
import utils
class DBHandler:
"""
......@@ -48,8 +49,7 @@ class DBHandler:
print("All data in activity table")
cursor = self.conn.execute("SELECT * from ACTIVITY ")
for row in cursor:
print(row)
sys.stdout.flush()
utils.logger.log(row)
return
def write_interval(self, interval_start, app_name, duration):
......@@ -57,8 +57,7 @@ class DBHandler:
Write an entry for the amount of time an application was active in an interval.
"""
query = f"INSERT INTO activity VALUES ({interval_start}, '{app_name}', {duration})"
print(query)
sys.stdout.flush()
utils.logger.log(query)
self.conn.execute(query)
self.conn.commit()
......@@ -67,7 +66,6 @@ class DBHandler:
Provided a date instance return all data present corresponding the whole week
for the date.
"""
sys.stdout.flush()
date = datetime(date.year, date.month, date.day, 0, 0, 0)
start = date - timedelta(days=date.weekday())
end = start + timedelta(days=7)
......
......@@ -7,6 +7,7 @@ from tkinter.ttk import *
from tkcalendar import DateEntry
import sys
import getopt
import utils
DEBUG = False
......@@ -169,5 +170,6 @@ if __name__ == '__main__':
Execution starts from here, Creating a ActivityTracker object, then it is set on mainloop().
"""
parse_cmdargs()
utils.logger.is_debug = DEBUG
tracker = ActivityTracker(DEBUG)
tracker.root.mainloop()
\ No newline at end of file
......@@ -147,5 +147,5 @@ class ActivityPlotter:
week_fig, screen_time_text = self.__get_week_plot()
day_fig = self.__get_day_plot()
app_fig = self.__get_app_plot()
print(self.curr_date, screen_time_text)
utils.logger.log(f"{self.curr_date}, {screen_time_text}")
return week_fig, day_fig, app_fig, screen_time_text
......@@ -201,11 +201,10 @@ if __name__ == '__main__':
parse_cmdargs()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
utils.logger.is_debug = DEBUG
utils.logger.log(f"Current deamon pid: {os.getpid()}")
print("Current deamon pid:", os.getpid())
sys.stdout.flush()
event_handler = init_eventhandler()
event_fetcher = EventFetcher()
while True:
event_fetcher.handle_xevent()
\ No newline at end of file
......@@ -25,13 +25,22 @@ def get_db_path(debug):
if debug:
if os.path.exists(os.path.dirname(DEV_DB_PATH)) == False:
os.mkdir(os.path.dirname(DEV_DB_PATH))
print("Using DB file: ", DEV_DB_PATH)
sys.stdout.flush()
logger.log(f"Using DB file: {DEV_DB_PATH}")
return DEV_DB_PATH
else:
if os.path.exists(os.path.dirname(DB_PATH)) == False:
os.mkdir(os.path.dirname(DB_PATH))
print("Using DB file: ", DB_PATH)
sys.stdout.flush()
logger.log(f"Using DB file: {DB_PATH}")
return DB_PATH
\ No newline at end of file
class Logger:
def __init__(self) -> None:
self.is_debug = False
def log(self, text):
if self.is_debug:
print(text)
sys.stdout.flush()
logger = Logger()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment