<commit_before>package luxe;

import haxe.io.Path;
import haxe.macro.Context;
import haxe.macro.Expr;

private enum LogError {
    RequireString(detail:String);
}

class Log {

        //default to `log`
    static var _level : Int = 1;
    static var _filter : Array<String>;
    static var _exclude : Array<String>;
    static var _log_width : Int = 16;

    macro public static function get_level() : haxe.macro.Expr {
        return macro $v{ ${luxe.Log._level} };
    }
    macro public static function get_filter() : haxe.macro.Expr {
        return macro $v{ ${luxe.Log._filter} };
    }
    macro public static function get_exclude() : haxe.macro.Expr {
        return macro $v{ ${luxe.Log._exclude} };
    }

    macro static function level( __level:Int ) : haxe.macro.Expr {

        _level = __level;

        return macro null;

    } //level

    macro static function filter( __filter:String ) : haxe.macro.Expr {

        _filter = __filter.split(',');

        var _index = 0;
        for(_f in _filter) {
            _filter[_index] = StringTools.trim(_f);
            _index++;
        }

        return macro null;

    } //filter

    macro static function exclude( __exclude:String ) : haxe.macro.Expr {

        _exclude = __exclude.split(',');

        var _index = 0;
        for(_e in _exclude) {
            _exclude[_index] = StringTools.trim(_e);
            _index++;
        }

        return macro null;

    } //exclude

    macro static function width( _width:Int ) : haxe.macro.Expr {

        _log_width = _width;

        return macro null;

    } //width

        //This macro uses the defined log level value to reject code that
        //shouldn't even exist at runtime , like low level debug information
        //and logging by injecting or not injecting code
    macro public static function log( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 0);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}i / $_context / ' + $value);
        }

        return macro null;

    } //log

    macro public static function _debug( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 1);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}d / $_context / ' + $value);
        }

        return macro null;

    } //_debug

    macro public static function _verbose( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 2);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}v / $_context / ' + $value);
        }

        return macro null;

    } //_verbose

    macro public static function _verboser( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 3);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}V / $_context / ' + $value);
        }

        return macro null;

    } //_verboser

    macro public static function assert(expr:Expr) {
        #if (debug || luxe_assert)
            var str = haxe.macro.ExprTools.toString(expr);
            return macro @:pos(Context.currentPos()) {
                if(!$expr) throw luxe.Log.DebugError.assertion('$str');
            }
        #end
        return macro null;
    } //assert


    macro public static function assertnull(value:Expr) {
        #if (debug || luxe_assert)
            var str = haxe.macro.ExprTools.toString(value);
            return macro @:pos(Context.currentPos()) {
                if($value == null) throw luxe.Log.DebugError.null_assertion('$str == null');
            }
        #end
        return macro null;
    } //assert


//Internal Helpers<commit_msg>Log; added def(value, default) which does if(value == null) value = default; return value; added reasons to assert and assert null for better error handling.<commit_after>package luxe;

import haxe.io.Path;
import haxe.macro.Context;
import haxe.macro.Expr;

private enum LogError {
    RequireString(detail:String);
}

class Log {

        //default to `log`
    static var _level : Int = 1;
    static var _filter : Array<String>;
    static var _exclude : Array<String>;
    static var _log_width : Int = 16;

    macro public static function get_level() : haxe.macro.Expr {
        return macro $v{ ${luxe.Log._level} };
    }
    macro public static function get_filter() : haxe.macro.Expr {
        return macro $v{ ${luxe.Log._filter} };
    }
    macro public static function get_exclude() : haxe.macro.Expr {
        return macro $v{ ${luxe.Log._exclude} };
    }

    macro static function level( __level:Int ) : haxe.macro.Expr {

        _level = __level;

        return macro null;

    } //level

    macro static function filter( __filter:String ) : haxe.macro.Expr {

        _filter = __filter.split(',');

        var _index = 0;
        for(_f in _filter) {
            _filter[_index] = StringTools.trim(_f);
            _index++;
        }

        return macro null;

    } //filter

    macro static function exclude( __exclude:String ) : haxe.macro.Expr {

        _exclude = __exclude.split(',');

        var _index = 0;
        for(_e in _exclude) {
            _exclude[_index] = StringTools.trim(_e);
            _index++;
        }

        return macro null;

    } //exclude

    macro static function width( _width:Int ) : haxe.macro.Expr {

        _log_width = _width;

        return macro null;

    } //width

        //This macro uses the defined log level value to reject code that
        //shouldn't even exist at runtime , like low level debug information
        //and logging by injecting or not injecting code
    macro public static function log( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 0);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}i / $_context / ' + $value);
        }

        return macro null;

    } //log

    macro public static function _debug( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 1);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}d / $_context / ' + $value);
        }

        return macro null;

    } //_debug

    macro public static function _verbose( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 2);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}v / $_context / ' + $value);
        }

        return macro null;

    } //_verbose

    macro public static function _verboser( value:Dynamic ) : Expr {

        var _file = Path.withoutDirectory(_get_log_file());
        var _context = Path.withoutExtension(_file).toLowerCase();
        var _spaces = _get_spacing(_file);

        for(meta in Context.getLocalClass().get().meta.get()) {
            if(meta.name == ':log_as') {
                var _str = switch(meta.params[0].expr) {
                    case EConst(CString(str)): _context = str;
                    default: throw LogError.RequireString('log_as meta requires a string constant like "name"');
                }
            }
        } //for each meta

        var _log = (_level > 3);

            if(_filter != null && (_filter.indexOf(_context) == -1)) {
                _log = false;
            }

            if(_exclude != null && (_exclude.indexOf(_context) != -1)) {
                _log = false;
            }

        if(_log) {
            return macro @:pos(Context.currentPos()) trace('${_spaces}V / $_context / ' + $value);
        }

        return macro null;

    } //_verboser

    macro public static function assert(expr:Expr, ?reason:String='') {
        #if (debug || luxe_assert)
            var str = haxe.macro.ExprTools.toString(expr);
                if(reason != '') str += ' ($reason)';
            return macro @:pos(Context.currentPos()) {
                if(!$expr) throw luxe.Log.DebugError.assertion('$str');
            }
        #end
        return macro null;
    } //assert


    macro public static function assertnull(value:Expr, ?reason:String='') {
        #if (debug || luxe_assert)
            var str = haxe.macro.ExprTools.toString(value);
            if(reason != '') reason = ' ($reason)';
            return macro @:pos(Context.currentPos()) {
                if($value == null) throw luxe.Log.DebugError.null_assertion('$str was null$reason');
            }
        #end
        return macro null;
    } //assert

    macro public static function def(value:Expr, def:Expr):Expr {
        return macro @:pos(Context.currentPos()) {
            if($value == null) $value = $def;
            $value;
        }
    }


//Internal Helpers
{'fraction_non_alphanumeric': 0.12612612612612611, 'fraction_numerical': 0.0029560810810810812, 'mean_word_length': 1.8327352472089313, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4456919', 'n_tokens_mistral': 4192, 'n_tokens_neox': 3889, 'n_words': 1006}