newsProxy / userio.py /
f7c8dfe 3 years ago
1 contributor
78 lines | 3.514kb
#
# Copyright (C) Actility, SA. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version
# 2 only, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License version 2 for more details (a copy is
# included at /legal/license.txt).
#
# You should have received a copy of the GNU General Public License
# version 2 along with this work; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
#
# Please contact Actility, SA.,  4, rue Ampere 22300 LANNION FRANCE
# or visit www.actility.com if you need additional
# information or have any questions.
#
from colorama import Fore, Back, Style
from time import gmtime, strftime
import configuration

name = "newsProxy"

def say(message):
    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
    with open(configuration.get_logfile(), "a+") as f:
        f.write(prefix + Style.DIM + message + Style.RESET_ALL+"\n")
        f.close()
        
    print(prefix + Style.DIM + message + Style.RESET_ALL)


def ok(message, detail=""):
    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
    level = Fore.GREEN + "[OK] " + Fore.RESET
    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
    with open(configuration.get_logfile(), "a+") as f:
        f.write(prefix + level + Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
        f.close()
    print(prefix + level + Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)


def warn(message, detail=""):
    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
    level = Fore.YELLOW + "[WARN] " + Fore.RESET
    with open(configuration.get_logfile(), "a+") as f:
        f.write(prefix  + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
        f.close()
    print(prefix  + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)


def error(message, detail=""):
    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
    level = Fore.RED + "[ERR] " + Fore.RESET
    with open(configuration.get_logfile(), "a+") as f:
        f.write(prefix + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
        f.close()
    print(prefix + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)


def debug(message, detail=""):
    if configuration.get_debug() != 0:
        date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
        prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
        level = Fore.MAGENTA + "[DEBUG] " + Fore.RESET
        with open(configuration.get_logfile(), "a+") as f:
            f.write(prefix + level +  Style.DIM + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
            f.close()
        print(prefix + level +  Style.DIM + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)