QGIS/python/plugins/processing/core/ProcessingLog.py

152 lines
5.5 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
2013-08-12 20:44:27 +02:00
ProcessingLog.py
2012-10-05 23:28:47 +02:00
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
2016-09-21 18:24:26 +02:00
from builtins import range
from builtins import object
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
import os
2012-09-15 18:25:25 +03:00
import codecs
import datetime
from processing.tools.system import userFolder
2013-08-12 20:44:27 +02:00
from processing.core.ProcessingConfig import ProcessingConfig
from qgis.core import QgsMessageLog
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import QCoreApplication
2016-09-21 18:24:26 +02:00
class ProcessingLog(object):
LOG_ERROR = 'ERROR'
LOG_INFO = 'INFO'
LOG_WARNING = 'WARNING'
LOG_ALGORITHM = 'ALGORITHM'
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
2012-09-15 18:25:25 +03:00
recentAlgs = []
@staticmethod
def logFilename():
2016-01-15 11:18:48 +01:00
logFilename = userFolder() + os.sep + 'processing.log'
if not os.path.isfile(logFilename):
with codecs.open(logFilename, 'w', encoding='utf-8') as logfile:
logfile.write('Started logging at ' +
datetime.datetime.now().strftime(ProcessingLog.DATE_FORMAT) + '\n')
2016-01-15 11:18:48 +01:00
return logFilename
2012-09-15 18:25:25 +03:00
@staticmethod
def addToLog(msgtype, msg):
try:
# It seems that this fails sometimes depending on the msg
# added. To avoid it stopping the normal functioning of the
# algorithm, we catch all errors, assuming that is better
2016-01-07 17:14:05 +01:00
# to miss some log info than breaking the algorithm.
if msgtype == ProcessingLog.LOG_ALGORITHM:
line = msgtype + '|' + datetime.datetime.now().strftime(
2015-10-02 21:06:19 +02:00
ProcessingLog.DATE_FORMAT) + '|' \
+ msg + '\n'
with codecs.open(ProcessingLog.logFilename(), 'a',
encoding='utf-8') as logfile:
logfile.write(line)
algname = msg[len('processing.runalg("'):]
algname = algname[:algname.index('"')]
if algname not in ProcessingLog.recentAlgs:
ProcessingLog.recentAlgs.append(algname)
recentAlgsString = ';'.join(ProcessingLog.recentAlgs[-6:])
ProcessingConfig.setSettingValue(
ProcessingConfig.RECENT_ALGORITHMS,
recentAlgsString)
else:
if isinstance(msg, list):
msg = '\n'.join([m for m in msg])
msgtypes = {ProcessingLog.LOG_ERROR: QgsMessageLog.CRITICAL,
ProcessingLog.LOG_INFO: QgsMessageLog.INFO,
ProcessingLog.LOG_WARNING: QgsMessageLog.WARNING, }
QgsMessageLog.logMessage(msg, ProcessingLog.tr("Processing"), msgtypes[msgtype])
2012-09-15 18:25:25 +03:00
except:
pass
@staticmethod
def getLogEntries():
entries = {}
errors = []
algorithms = []
warnings = []
info = []
2016-01-15 11:18:48 +01:00
with open(ProcessingLog.logFilename()) as f:
lines = f.readlines()
for line in lines:
line = line.strip('\n').strip()
tokens = line.split('|')
text = ''
2012-09-15 18:25:25 +03:00
for i in range(2, len(tokens)):
text += tokens[i] + '|'
2013-08-12 20:44:27 +02:00
if line.startswith(ProcessingLog.LOG_ERROR):
2012-09-15 18:25:25 +03:00
errors.append(LogEntry(tokens[1], text))
2013-08-12 20:44:27 +02:00
elif line.startswith(ProcessingLog.LOG_ALGORITHM):
2012-09-15 18:25:25 +03:00
algorithms.append(LogEntry(tokens[1], tokens[2]))
2013-08-12 20:44:27 +02:00
elif line.startswith(ProcessingLog.LOG_WARNING):
2012-09-15 18:25:25 +03:00
warnings.append(LogEntry(tokens[1], text))
2013-08-12 20:44:27 +02:00
elif line.startswith(ProcessingLog.LOG_INFO):
2012-09-15 18:25:25 +03:00
info.append(LogEntry(tokens[1], text))
2013-08-12 20:44:27 +02:00
entries[ProcessingLog.LOG_ALGORITHM] = algorithms
2012-09-15 18:25:25 +03:00
return entries
@staticmethod
def getRecentAlgorithms():
recentAlgsSetting = ProcessingConfig.getSetting(
ProcessingConfig.RECENT_ALGORITHMS)
2012-09-15 18:25:25 +03:00
try:
2013-08-12 20:44:27 +02:00
ProcessingLog.recentAlgs = recentAlgsSetting.split(';')
2012-09-15 18:25:25 +03:00
except:
pass
2013-08-12 20:44:27 +02:00
return ProcessingLog.recentAlgs
2012-09-15 18:25:25 +03:00
@staticmethod
def clearLog():
2013-08-12 20:44:27 +02:00
os.unlink(ProcessingLog.logFilename())
2012-09-15 18:25:25 +03:00
@staticmethod
def saveLog(fileName):
entries = ProcessingLog.getLogEntries()
with codecs.open(fileName, 'w', encoding='utf-8') as f:
2016-09-27 19:51:06 +02:00
for k, v in list(entries.items()):
for entry in v:
f.write('%s|%s|%s\n' % (k, entry.date, entry.text))
@staticmethod
def tr(string, context=''):
if context == '':
context = 'ProcessingLog'
return QCoreApplication.translate(context, string)
2016-09-21 18:24:26 +02:00
class LogEntry(object):
2012-09-15 18:25:25 +03:00
def __init__(self, date, text):
self.date = date
self.text = text