U uZIe@sddlZddlZddlZddlZddlZddlZejeje Z ej e dZ e de ZejZedZejdkrzdZejfddZdd d ZGd d d eZGdddeZdS)Nz helperlib.py helperlibz /log/dsc.log omsconfigz*/var/opt/microsoft/omsconfig/omsconfig.logcCs||ddS)N )write)sfiler4/opt/microsoft/omsconfig/Scripts/python3/nxDSCLog.pyPrintsr rcCs4zt||d}WndtdfYSX|dfS)z2 This context ensures the file is closed. utf8NIOError)codecsopen Exception)filenamemodefrrr opened_w_errors rc@s(eZdZefddZddZddZdS)DSCLogcCs6d|_||_tjdkr,tdtd||_dS)N))rZFATAL)ZERROR)ZWARNING)INFO)DEBUG)VERBOSErz mkdir -p z/log) levelsGetCurrentLogLevel current_levelrCONFIG_SYSCONFDIR_DSCossystemVarDir file_path)selflogpathrrr __init__)s   zDSCLog.__init__c Csntj}|jddt|jd}|dks:t|dkr>dS|dkrL|j}t|tkr||}d}|j D]\}}||krf|}qf|dks|dkst|dkrdS||jkrdSt }d|j |j |j|j|j|j|j |d||f }d} zNt|jd\} } | r td |jd t| tjd n| || Wn4| td |jd t| tjd YnXdS) N__file__()rrrz*%04u/%02u/%02u %02u:%02u:%02u: %s: %s: %s azException opening logfile z Error: )r)inspectZ currentframef_back f_globalsstrf_linenolenr typertime localtimetm_yeartm_montm_mdaytm_hourtm_mintm_secrr%r sysstderrrclose) r&Z log_levelmessageZ last_frameZplacetZnumZstrnglineerrorFrrr Log1sn        z DSCLog.LogcCsdS)Nrrr&rrr rUszDSCLog.GetCurrentLogLevelN)__name__ __module__ __qualname__LogFiler(rDrrrrr r's $rc@s0eZdZefddZddZddZddZd S) ConsoleAndFileLoggercCstj|_||_d|_dS)NF)r<stdoutconsoler' errorreported)r&pathrrr r(YszConsoleAndFileLogger.__init__cCs|j|zbt|jdd}zBz||Wn.|jsX|jd|jdd|_YnXW5|XWn.|js|jd|jdd|_YnXdS)Nr,r z!Failed to write the dsc log file rTz Failed to open the dsc log file )rLrrrr'r>rM)r&r?Z file_handlerrr r^s zConsoleAndFileLogger.writecCsdSNrrErrr flushpszConsoleAndFileLogger.flushcCs |jt_dSrO)rLr<rKrErrr __del__rszConsoleAndFileLogger.__del__N)rFrGrHrIr(rrPrQrrrr rJXs rJ)r )r"r<r4r-rimprNdirnamerealpathr)ZscriptFolderPathjoinZfullPath load_sourcerPYTHON_PID_DIRr$rIr!r=r robjectrrJrrrr s    1