<commit_before>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use base
import io/FileWriter

DebugLevel: enum {
	Everything
	Debug
	Notification
	Warning
	Recoverable
	Message
	Critical
}

Debug: class {
	_level: static DebugLevel = DebugLevel Everything
	_printFunction: static Func (String) = func (s: String) { println(s) }
	initialize: static func (f: Func (String)) {
		This _printFunction = f
	}
	print: static func (string: String, level := DebugLevel Everything) {
		if (This _level == level || (This _level == DebugLevel Everything))
			This _printFunction(string)
	}
	print: static func ~text (text: Text, level := DebugLevel Everything) {
		string := text toString()
		This print(string, level)
		string free()
	}
	error: static func (message: String) {
		This print(message)
		raise(message)
	}
	error: static func ~text (message: Text) {
		string := message toString()
		This error(string)
		string free()
	}
	kean_base_debug_registerCallback: unmangled static func (print: Pointer) {
		f := (print, null) as Func (Char*)
		This initialize(func (s: String) { f(s toCString()) })
	}
}
<commit_msg>Removed memory leaks in Debug class<commit_after>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use base
import io/FileWriter

DebugLevel: enum {
	Everything
	Debug
	Notification
	Warning
	Recoverable
	Message
	Critical
}

Debug: class {
	_level: static DebugLevel = DebugLevel Everything
	_printFunction: static Func (String) = func (s: String) { println(s) }
	initialize: static func (f: Func (String)) {
		(This _printFunction as Closure) free()
		This _printFunction = f
	}
	print: static func (string: String, level := DebugLevel Everything) {
		if (This _level == level || (This _level == DebugLevel Everything))
			This _printFunction(string)
	}
	print: static func ~text (text: Text, level := DebugLevel Everything) {
		string := text toString()
		This print(string, level)
		string free()
	}
	error: static func (message: String) {
		This print(message)
		raise(message)
	}
	error: static func ~text (message: Text) {
		string := message toString()
		This error(string)
		string free()
	}
	free: static func ~all {
		(This _printFunction as Closure) free()
	}
	kean_base_debug_registerCallback: unmangled static func (print: Pointer) {
		f := (print, null) as Func (Char*)
		This initialize(func (s: String) { f(s toCString()) })
	}
}

GlobalCleanup register(|| Debug free~all())

{'fraction_non_alphanumeric': 0.10539389638041163, 'fraction_numerical': 0.0028388928317956, 'mean_word_length': 4.210720887245841, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6053231', 'n_tokens_mistral': 913, 'n_tokens_neox': 869, 'n_words': 325}