11package codename .backend .system ;
22
3- import flixel .system .debug .log .LogStyle ;
4- import flixel .system .frontEnds .LogFrontEnd ;
53import haxe .Log ;
6- import codename .backend .utils .NativeAPI ;
7- import codename .backend .utils .NativeAPI .ConsoleColor ;
4+ import haxe .PosInfos ;
5+ import lime .app .Application ;
6+ using codename .backend .system .Logs .LogTools ;
87
9- class Logs {
10- private static var __showing : Bool = false ;
8+ class AlertParams
9+ {
10+ public var content : String = ' None' ;
11+ public var title : Null <String > = null ;
1112
12- public static var nativeTrace = Log .trace ;
13- public static function init () {
14- Log .trace = function (v : Dynamic , ? infos : Null <haxe. PosInfos >) {
15- var data = [
16- logText (' ${infos .fileName }: ${infos .lineNumber }: ' , CYAN ),
17- logText (Std .string (v ))
18- ];
13+ public function new (content : String = ' None' , ? title : Null <String >)
14+ {
15+ this .content = content ;
16+ this .title = title ;
17+ }
18+ }
1919
20- if (infos .customParams != null ) {
21- for (i in infos .customParams ) {
22- data .push (
23- logText (" ," + Std .string (i ))
24- );
25- }
26- }
27- __showInConsole (prepareColoredTrace (data , TRACE ));
28- };
20+ class LogTools
21+ {
22+ @:deprecated (' The level.toLevel() function will soon be removed from backend.LogTools.' ) public static inline function toLevel (level : Level ): String
23+ {
24+ return levelToString (level );
25+ }
2926
30- LogUtils .onLogs = function (data , style , fireOnce ) {
31- var prefix = " [FLIXEL]" ;
32- var color : ConsoleColor = LIGHTGRAY ;
33- var level : Level = INFO ;
34- if (style == LogStyle .CONSOLE ) // cant place a switch here as these arent inline values - Nex
35- {
36- prefix = " > " ;
37- color = WHITE ;
38- level = INFO ;
39- }
40- else if (style == LogStyle .ERROR )
41- {
42- prefix = " [FLIXEL]" ;
43- color = RED ;
44- level = ERROR ;
45- }
46- else if (style == LogStyle .NORMAL )
47- {
48- prefix = " [FLIXEL]" ;
49- color = WHITE ;
50- level = INFO ;
51- }
52- else if (style == LogStyle .NOTICE )
53- {
54- prefix = " [FLIXEL]" ;
55- color = GREEN ;
56- level = VERBOSE ;
57- }
58- else if (style == LogStyle .WARNING )
59- {
60- prefix = " [FLIXEL]" ;
61- color = YELLOW ;
62- level = WARNING ;
63- }
27+ public static inline function levelToString (level : Level ): String
28+ {
29+ switch (level )
30+ {
31+ case INFO | NONE :
32+ return ' [ INFO ]' ;
33+ case WARNING :
34+ return ' [ WARNING ]' ;
35+ case ERROR :
36+ return ' [ ERROR ]' ;
37+ default :
38+ return ' [ MISC ]' ;
39+ }
40+ }
6441
65- var d : Dynamic = data ;
66- if (! (d is Array ))
67- d = [d ];
68- var a : Array <Dynamic > = d ;
69- var strs = [for (e in a ) Std .string (e )];
70- for (e in strs )
71- {
72- Logs .trace (' $prefix $e ' , level , color );
73- }
74- };
75- }
42+ public static inline function spawnAlertWindow (params : AlertParams , ? infos : haxe. PosInfos )
43+ {
44+ Logs .traceNew (params .content , ERROR , infos );
45+ Application .current .window .alert (params .content , params .title );
46+ }
47+ }
7648
77- public static function prepareColoredTrace (text : Array <LogText >, level : Level = INFO ) {
78- var time = Date .now ();
79- var superCoolText = [
80- logText (' [ ' ),
81- logText (' ${Std .string (time .getHours ()).addZeros (2 )}: ${Std .string (time .getMinutes ()).addZeros (2 )}: ${Std .string (time .getSeconds ()).addZeros (2 )}' , DARKMAGENTA ),
82- logText (' |' ),
83- switch (level )
84- {
85- case WARNING :
86- logText (' WARNING ' , DARKYELLOW );
87- case ERROR :
88- logText (' ERROR ' , DARKRED );
89- case TRACE :
90- logText (' TRACE ' , GRAY );
91- case VERBOSE :
92- logText (' VERBOSE ' , DARKMAGENTA );
93- default :
94- logText (' INFORMATION ' , CYAN );
95- },
96- logText (' ] ' )
97- ];
98- for (k => e in superCoolText )
99- text .insert (k , e );
100- return text ;
101- }
49+ class Logs
50+ {
51+ public static var nativeTrace = Log .trace ;
10252
103- public static function logText (text : String , color : ConsoleColor = LIGHTGRAY ): LogText {
104- return {
105- text : text ,
106- color : color
107- };
108- }
53+ public static function init ()
54+ {
55+ Log .trace = traceOverride ;
56+ }
10957
110- public static function __showInConsole (text : Array <LogText >) {
111- #if (sys && !mobile)
112- while (__showing ) {
113- Sys .sleep (0.05 );
114- }
115- __showing = true ;
116- for (t in text ) {
117- NativeAPI .setConsoleColors (t .color );
118- Sys .print (t .text );
119- }
120- NativeAPI .setConsoleColors ();
121- Sys .print (" \r\n " );
122- __showing = false ;
123- #elseif mobile
124- while (__showing ) {
125- Sys .sleep (0.05 );
126- }
127- __showing = true ;
128- @:privateAccess
129- Sys .print ([for (t in text ) t .text ].join (" " ));
130- __showing = false ;
131- #else
132- @:privateAccess
133- nativeTrace ([for (t in text ) t .text ].join (" " ));
134- #end
135- }
58+ static function levelToInt (l : Level ): Int
59+ {
60+ switch (l )
61+ {
62+ case INFO :
63+ return 0 ;
64+ case MISC :
65+ return 1 ;
66+ case NONE :
67+ return 2 ;
68+ case WARNING :
69+ return 3 ;
70+ case ERROR :
71+ return 4 ;
72+ }
73+ }
13674
137- public static function traceColored (text : Array <LogText >, level : Level = INFO )
138- __showInConsole (prepareColoredTrace (text , level ));
75+ static function traceOverride (v : Dynamic , ? infos : PosInfos ): Void traceNew (v , NONE , infos );
13976
140- public static function trace (text : String , level : Level = INFO , color : ConsoleColor = LIGHTGRAY ) {
141- traceColored ([
142- {
143- text : text ,
144- color : color
145- }
146- ], level );
147- }
77+ public static function traceNew (v : Dynamic , level : Level , ? infos : PosInfos ): Void
78+ {
79+ var str = ' ${level .toLevel ()} : ' + Log .formatOutput (v , infos ).replace (' \n\n ' , ' \n ' ).replace (' \n ' , ' \n ${level .toLevel ()} : ${infos .fileName }: ${infos .lineNumber }: ' );
80+ #if js
81+ if (js. Syntax .typeof (untyped console ) != " undefined" && (untyped console ).log != null )
82+ (untyped console ).log (str );
83+ #elseif lua
84+ untyped __define_feature__ (" use._hx_print" , _hx_print (str ));
85+ #elseif sys
86+ Sys .println (str );
87+ #else
88+ throw new haxe.exceptions. NotImplementedException ()
89+ #end
90+ }
14891}
14992
150- enum abstract Level (Int ) {
151- var INFO = 0 ;
152- var WARNING = 1 ;
153- var ERROR = 2 ;
154- var TRACE = 3 ;
155- var VERBOSE = 4 ;
93+ enum abstract TraceStyle (String ) {
94+ var ALL = ' ALL' ;
95+ var WARNINGS_PLUS = ' WARNINGS+' ;
96+ var ERRORS_ONLY = ' ERRORS_ONLY' ;
97+ var NONE = ' NONE' ;
15698}
15799
158- typedef LogText = {
159- var text : String ;
160- var color : ConsoleColor ;
100+ enum abstract Level (Int )
101+ {
102+ var INFO = 0 ;
103+ var MISC = 1 ;
104+ var NONE = 2 ;
105+ var WARNING = 3 ;
106+ var ERROR = 4 ;
161107}
0 commit comments