Skip to content

Instantly share code, notes, and snippets.

@foreignsasquatch
Last active March 12, 2022 11:40
Show Gist options
  • Save foreignsasquatch/5a3e5350a6db50fd6752e87bcc547890 to your computer and use it in GitHub Desktop.
Save foreignsasquatch/5a3e5350a6db50fd6752e87bcc547890 to your computer and use it in GitHub Desktop.
Small logging helper for haxe
/*
* Log.hx
*
* LICENSE: zlib/libpng
*
* hxRaylib is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software:
*
* Copyright (c) 2022 Ratul Krisna (@ForeignSasquatch)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
*/
import haxe.PosInfos;
enum Level {
INFO;
ERROR;
DEBUG;
}
class Log {
static var output:Array<String> = [];
static var end = '\x1b[0m';
public static function info(v:Dynamic, ?pos:PosInfos) {
log(v, INFO, pos);
}
public static function debug(v:Dynamic, ?pos:PosInfos) {
log(v, DEBUG, pos);
}
public static function error(v:Dynamic, ?pos:PosInfos) {
log(v, ERROR, pos);
}
public static function log(i:Dynamic, l:Level, ?pos:PosInfos) {
if(l == Level.INFO)
Sys.println("\033[38;5;244m" + DateTools.format(Date.now(), "%H:%M:%S") + end + '\x1b[32m' + " [INFO] " + end + "\033[38;5;239m" + pos.fileName + ":" + pos.lineNumber + end + " " + "\033[38;5;248m" + i + end);
if(l == Level.ERROR)
Sys.println("\033[38;5;244m" + DateTools.format(Date.now(), "%H:%M:%S") + end + "\033[38;5;160m" + " [ERROR] " + end + "\033[38;5;239m" + pos.fileName + ":" + pos.lineNumber + end + " " + "\033[38;5;248m" + i + end);
if(l == Level.DEBUG)
Sys.println("\033[38;5;244m" + DateTools.format(Date.now(), "%H:%M:%S") + end + "\033[38;5;75m" + " [DEBUG] " + end + "\033[38;5;239m" + pos.fileName + ":" + pos.lineNumber + end + " " + "\033[38;5;248m" + i + end);
var out = DateTools.format(Date.now(), "%H:%M:%S") + " " + l + " " + i;
output.push(out);
}
public static function save(f:String) {
var s = output.join("\n");
sys.io.File.saveContent(f, s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment