var fso = new ActiveXObject("Scripting.FileSystemObject") /////////////////////////////////////////////////////////////////////////////// function OnInit(data){ data.name = "Command.Generic: LogCommand"; data.desc = "LogWrapper for internal/script commands."; data.copyright = "tbone"; data.version = "0.4.2"; data.default_enable = true; var cmd = data.AddCommand(); cmd.name = "LogCommand"; cmd.method = "Command_LogCommand"; cmd.desc = data.desc; cmd.label = "LogCommand"; cmd.template = "COMMAND/K,LOGNAME/K,LOGTOFOLDER/K,LOGFOLDER/K,ID/K,GOLOGS/S,PARAMS/R,"; data.config["Log.ToFolder"] = true; data.config["Log.ToFolder.Path"] = "/home\\logs"; data.config["Log.FailedItems"] = true; data.config["Log.ToConsole"] = true; data.config["Log.ToConsole.Debug"] = false; } /////////////////////////////////////////////////////////////////////////////// function Command_LogCommand(data) { Debug("LogCommand():"); Debug(" data.cmdline: "+data.cmdline); var date = new Date(); var dateFormatted = FormatDate(date); var dateFormattedFS = dateFormatted.replace(/\./g,"_"); var timeFormatted = FormatTime(date); var command = GetParameter(data, "COMMAND", null, null); var logName = GetParameter(data, "LOGNAME", null, null); //overrides command name for log name var logToFolder = GetParameter(data, "LOGTOFOLDER", "Log.ToFolder", true); var logToConsole = GetParameter(data, "LOGTOCONSOLE", "Log.ToConsole", true); var logFailed = GetParameter(data, "LOGFAILEDITEMS", "Log.FailedItems", true); var goLogs = GetParameter(data, "GOLOGS", null, false); var logID = GetParameter(data, "ID", null, ""); var commandParams = GetParameter(data, "PARAMS", null, ""); if (!command && !goLogs) { Debug(" Can not run command, param [COMMAND] not given."); return false; } if (!logName) logName=command; var logger = null; if (logToFolder){ var logPath = DOpus.FSUtil.Resolve( GetParameter(data, "LOGFOLDER", "Log.ToFolder.Path", "/home\\logs")+"\\"+dateFormattedFS); if (!CreateFolders(logPath)){ logToFolder = false; Debug(" Can not log to folder, path ["+logPath+"] failed to create."); } } if (goLogs){ DOpus.NewCommand.RunCommand('GO "'+logPath+'"'); return true; } var logger = null; if (logToFolder) logger = new DebugOutput().mute(); //no console output from logger itself if (logger) logger.append(logPath+"\\"+dateFormattedFS+"_"+logName+"_log.csv"); //special clipboard paste handling if(command.toLowerCase()=="clipboard" && commandParams.toLowerCase().indexOf("paste")!=-1){ Debug(" Using files from clipboard"); //CLIPBOARD PASTE called, using clipboard files instead of current lister content data.func.command.ClearFiles(); data.func.command.AddFilesFromClipboard(); } if (commandParams!="") command = command+" "+commandParams; command = PrepCSVCell(command); data.func.command.SetQualifiers(data.func.qualifiers); Debug(" data.func.qualifiers: " +data.func.qualifiers); data.func.command.RunCommand(command); if (!(logger || logToConsole)) return true; var items = data.func.command.files; var itemsEnum = new Enumerator(items); var typeFolder = "folder", typeFile = "file", type = ""; Debug(" data.func.command.files.count: " + data.func.command.files.count); for(var item;!itemsEnum.atEnd();) { var item = itemsEnum.item(); itemsEnum.moveNext(); type = typeFile; if (item.is_dir) type = typeFolder; var status = "successful" if (item.failed) { if (!logFailed) continue; status = "fail, skip"; } if (logger) logger.put( dateFormatted+";"+ timeFormatted+";"+ command+";"+ logID+";"+ data.func.qualifiers+";"+ type+";"+ status+";"+ PrepCSVCell(item.name)+";"+ PrepCSVCell(item.path)+";"+ PrepCSVCell(item.realpath)+";"+ PrepCSVCell(data.func.sourcetab.path)+";"+ PrepCSVCell(data.func.desttab.path) ); if (logToConsole) DOpus.Output(command+" "+status+" ["+item.name+"]"); } return true; } /////////////////////////////////////////////////////////////////////////////// function FormatTime(date){ var hour = date.getHours(); if (hour<10) hour="0"+hour; var min = date.getMinutes(); if (min<10) min="0"+min; var secs = date.getSeconds(); if (secs<10) secs="0"+secs; return hour+":"+min+":"+secs; } /////////////////////////////////////////////////////////////////////////////// function FormatDate(date){ var day = date.getDate(); if (day<10) day="0"+day; var month = date.getMonth()+1; if (month<10) month="0"+month; var year = date.getFullYear(); return year+"."+month+"."+day; } /////////////////////////////////////////////////////////////////////////////// function PrepCSVCell(cellcontent){ cellcontent = new String(cellcontent); if (cellcontent.indexOf(";")!=-1) cellcontent = '"'+cellcontent+'"'; return cellcontent; } /////////////////////////////////////////////////////////////////////////////// function CreateFolders(fullPath){ //version = 0.5.1; fullPath = new String(fullPath); if (fso==undefined) var fso = new ActiveXObject("Scripting.FileSystemObject"); if (fso.FolderExists(fullPath)) return true; var drive = fullPath.substring(0,3); var dirs = fullPath.split("\\"); var path = "" for(var i=1;i0) this.inc(); if (indent<0) this.dec(); } /////////////////////////////////////////////////////////////////////////// this._pad = function( length) { var str = ""; while (str.length < length) str+=this._indentChar; return str; } /////////////////////////////////////////////////////////////////////////// this.level = function(level){ if (level==undefined) level=4; if (level>-1) this._level=level; if (level>6) this._level=6; return this; } /////////////////////////////////////////////////////////////////////////// this._conOut = function(txt){ if (this._inDOP && !this._muted) Debug(txt); if (this._inWSH && !this._muted) WScript.Echo(txt); } /////////////////////////////////////////////////////////////////////////// this.out = function(txt, indent, type){ if (indent==undefined) indent=0; if (type==undefined) type=""; var indTmp = this._indent; if (type!="" && indTmp==0) indTmp=1; if (this._level>0){ var output = type+ this._pad(indTmp*this._indentWidth-type.length) +txt; if (this._console) this._conOut(output); if (this._buffered) this.outBuffer[this.outBuffer.length]=output; if (this._file) this._fileOut(output); } this._ind(indent); return this; } /////////////////////////////////////////////////////////////////////////// this._fileOut = function(txt){ if (this._save && this._fso.FileExists(this._filePath)) this._fso.DeleteFile(this._filePath); this._save = false; var file = this._fso.OpenTextFile(this._filePath,8,true); file.WriteLine(txt); file.Close(); } /////////////////////////////////////////////////////////////////////////// this.append = function(filepath){ if (filepath==undefined) throw "No filepath given."; if (fso==undefined) this._fso = new ActiveXObject("Scripting.FileSystemObject"); else this._fso = fso; this._filePath = filepath; this._file = true; return this; } /////////////////////////////////////////////////////////////////////////// this.save = function(filepath ){ this._save = true; return this.append(filepath); } /////////////////////////////////////////////////////////////////////////// this.dmp = function(txt, indent){ if (this._level+1>6||(this._keepInds&&indent)) this.out(txt, indent); return this; } /////////////////////////////////////////////////////////////////////////// this.trc = function(txt, indent){ if (this._level+1>5||(this._keepInds&&indent)) this.out(txt, indent); return this; } /////////////////////////////////////////////////////////////////////////// this.put = function(txt, indent){ if (this._level+1>4||(this._keepInds&&indent)) this.out(txt, indent); return this; } /////////////////////////////////////////////////////////////////////////// this.inf = function(txt, indent){ if (this._level+1>3||(this._keepInds&&indent)) this.out(txt, indent, "I"); return this; } /////////////////////////////////////////////////////////////////////////// this.wrn = function(txt, indent){ if (this._level+1>2||(this._keepInds&&indent)) this.out(txt, indent, "W"); return this; } /////////////////////////////////////////////////////////////////////////// this.err = function(txt, indent){ if (this._level+1>1||(this._keepInds&&indent)) this.out(txt, indent, "E"); return this; } /////////////////////////////////////////////////////////////////////////// this.xit = function(txt, indent){ if (this._level+1>0||(this._keepInds&&indent)) this.out(txt, indent, "X"); return this; } }