Fix psutil version mismatch and append logs

This commit is contained in:
Gobinath 2017-04-28 10:26:53 -04:00
parent dbd1f1bce7
commit 2c0db7fcb7
2 changed files with 38 additions and 14 deletions

View File

@ -21,6 +21,7 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gdk, GLib
from html.parser import HTMLParser
from distutils.version import LooseVersion
from logging.handlers import RotatingFileHandler
import babel.dates, os, errno, re, subprocess, threading, logging, locale, json, shutil, pyaudio, wave
bin_directory = os.path.dirname(os.path.realpath(__file__))
@ -31,6 +32,7 @@ config_file_path = os.path.join(config_directory, 'safeeyes.json')
style_sheet_path = os.path.join(config_directory, 'style/safeeyes_style.css')
system_config_file_path = os.path.join(bin_directory, "config/safeeyes.json")
system_style_sheet_path = os.path.join(bin_directory, "config/style/safeeyes_style.css")
log_file_path = os.path.join(config_directory, 'safeeyes.log')
def play_notification():
"""
@ -361,6 +363,30 @@ def __initialize_safeeyes():
shutil.copy2(system_style_sheet_path, style_sheet_path)
def intialize_logging():
"""
Initialize the logging framework using the Safe Eyes specific configurations.
"""
# Create the directory to store log file if not exist
if not os.path.exists(config_directory):
try:
os.makedirs(config_directory)
except:
pass
# Configure logging.
log_formatter = logging.Formatter('%(asctime)s [%(levelname)s]:[%(threadName)s] %(message)s')
# Apped the logs and overwrite once reached 5MB
handler = RotatingFileHandler(log_file_path, mode='a', maxBytes=5*1024*1024, backupCount=2, encoding=None, delay=0)
handler.setFormatter(log_formatter)
handler.setLevel(logging.INFO)
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
root_logger.addHandler(handler)
def read_config():
"""
Read the configuration from the config directory.

View File

@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, gi, json, dbus, logging, operator, psutil
import os, gi, json, dbus, logging, operator, psutil, sys
from threading import Timer
from dbus.mainloop.glib import DBusGMainLoop
gi.require_version('Gtk', '3.0')
@ -33,7 +33,6 @@ from safeeyes.TrayIcon import TrayIcon
from safeeyes import Utility
# Define necessary paths
log_file_path = os.path.join(Utility.config_directory, 'safeeyes.log')
break_screen_glade = os.path.join(Utility.bin_directory, "glade/break_screen.glade")
settings_dialog_glade = os.path.join(Utility.bin_directory, "glade/settings_dialog.glade")
about_dialog_glade = os.path.join(Utility.bin_directory, "glade/about_dialog.glade")
@ -203,10 +202,16 @@ def running():
"""
process_count = 0
for proc in psutil.process_iter():
if not proc.cmdline: continue
try:
# Check if safeeyes is in process arguments
cmd_line = proc.cmdline()
if 'python3' in cmd_line[0] and 'safeeyes' in cmd_line[1]:
if callable(proc.cmdline):
# Latest psutil has cmdline function
cmd_line = proc.cmdline()
else:
# In older versions cmdline was a list object
cmd_line = proc.cmdline
if 'python3' in cmd_line[0] and ('safeeyes' in cmd_line[1] or 'safeeyes' in cmd_line):
process_count += 1
if process_count > 1:
return True
@ -221,16 +226,9 @@ def main():
"""
Start the Safe Eyes.
"""
# Initialize the logging
Utility.intialize_logging()
# Create the directory to store log file if not exist
if not os.path.exists(Utility.config_directory):
try:
os.makedirs(Utility.config_directory)
except:
pass
# Configure logging.
logging.basicConfig(format='%(asctime)s [%(levelname)s]:[%(threadName)s] %(message)s', filename=log_file_path, filemode='w', level=logging.INFO)
logging.info("Starting Safe Eyes")
if not running():
@ -273,7 +271,7 @@ def main():
Gtk.main()
else:
logging.info('SafeEyes is already running')
logging.info('Another instance of safeeyes is already running')
sys.exit(0)
if __name__ == '__main__':