this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: add logger

+127 -30
+102
src/core/logger.odin
··· 1 + package core 2 + 3 + import "core:fmt" 4 + import "core:os" 5 + 6 + Log_Level :: enum { 7 + INFO, 8 + DEBUG, 9 + ERROR, 10 + } 11 + 12 + Logger :: struct { 13 + debug_enabled: bool, 14 + initialized: bool, 15 + } 16 + 17 + logger := Logger{} 18 + 19 + ANSI_RESET :: "\x1b[0m" 20 + ANSI_RED :: "\x1b[31m" 21 + ANSI_DIM :: "\x1b[2m" 22 + ANSI_CYAN :: "\x1b[36m" 23 + ANSI_GREEN :: "\x1b[32m" 24 + ANSI_YELLOW :: "\x1b[33m" 25 + ANSI_MAGENTA :: "\x1b[35m" 26 + 27 + is_truthy :: proc(value: string) -> bool { 28 + return value == "1" || value == "true" || value == "TRUE" || value == "yes" || value == "YES" || value == "on" || value == "ON" 29 + } 30 + 31 + is_debug_env :: proc() -> bool { 32 + debug_value := os.get_env("DEBUG", context.temp_allocator) 33 + defer delete(debug_value) 34 + 35 + app_env := os.get_env("APP_ENV", context.temp_allocator) 36 + defer delete(app_env) 37 + 38 + return is_truthy(debug_value) || app_env == "debug" || app_env == "DEBUG" 39 + } 40 + 41 + set_debug_enabled :: proc(enabled: bool) { 42 + logger.debug_enabled = enabled 43 + logger.initialized = true 44 + } 45 + 46 + init_logger :: proc() { 47 + logger.debug_enabled = is_debug_env() 48 + logger.initialized = true 49 + } 50 + 51 + ensure_logger :: proc() { 52 + if !logger.initialized { 53 + init_logger() 54 + } 55 + } 56 + 57 + colorize :: proc(s, color: string) -> string { 58 + return fmt.tprintf("%s%s%s", color, s, ANSI_RESET) 59 + } 60 + 61 + logf :: proc(level: Log_Level, format: string, args: ..any) { 62 + log(level, fmt.tprintf(format, ..args)) 63 + } 64 + 65 + log :: proc(level: Log_Level, message: string) { 66 + ensure_logger() 67 + 68 + switch level { 69 + case .INFO: 70 + fmt.printfln("[INFO] %s", message) 71 + case .DEBUG: 72 + if logger.debug_enabled { 73 + fmt.printfln("[DEBUG] %s", message) 74 + } 75 + case .ERROR: 76 + fmt.printfln("%s[ERROR] %s%s", ANSI_RED, message, ANSI_RESET) 77 + } 78 + } 79 + 80 + log_info :: proc(message: string) { 81 + log(.INFO, message) 82 + } 83 + 84 + log_debug :: proc(message: string) { 85 + log(.DEBUG, message) 86 + } 87 + 88 + log_error :: proc(message: string) { 89 + log(.ERROR, message) 90 + } 91 + 92 + log_infof :: proc(format: string, args: ..any) { 93 + logf(.INFO, format, ..args) 94 + } 95 + 96 + log_debugf :: proc(format: string, args: ..any) { 97 + logf(.DEBUG, format, ..args) 98 + } 99 + 100 + log_errorf :: proc(format: string, args: ..any) { 101 + logf(.ERROR, format, ..args) 102 + }
+25 -30
src/formats/flac.odin
··· 10 10 import "core:strconv" 11 11 import "core:strings" 12 12 import "core:testing" 13 + import app_core "../core" 13 14 14 15 ReadError :: enum { 15 16 UnknownError, ··· 20 21 Invalid_Vendor_String, 21 22 Unable_To_Read_Field_Length, 22 23 } 24 + 23 25 24 26 VorbisComment :: struct { 25 27 title: string, ··· 60 62 return false 61 63 } 62 64 63 - fmt.printfln("Marker: %v", string(marker)) 65 + app_core.log_debugf("%s %s", app_core.colorize("marker", app_core.ANSI_CYAN), app_core.colorize(string(marker), app_core.ANSI_GREEN)) 64 66 65 67 if string(marker) != "fLaC" { 66 68 return false ··· 82 84 } 83 85 84 86 parse_header :: proc(arr: []byte) -> Header { 85 - fmt.printfln("parse_header called, len=%d, data=%v", len(arr), arr) 87 + app_core.log_debugf("%s len=%d data=%v", app_core.colorize("parse_header", app_core.ANSI_CYAN), len(arr), arr) 86 88 87 89 assert(len(arr) >= 4) 88 90 89 - fmt.printfln( 90 - "Before return - arr[0]=%d arr[1]=%d arr[2]=%d arr[3]=%d", 91 - arr[0], 92 - arr[1], 93 - arr[2], 94 - arr[3], 95 - ) 91 + app_core.log_debugf("%s b0=%d b1=%d b2=%d b3=%d", app_core.colorize("header-bytes", app_core.ANSI_DIM), arr[0], arr[1], arr[2], arr[3]) 96 92 97 93 return Header { 98 94 is_last = arr[0] & 0x80 != 0, ··· 102 98 } 103 99 104 100 parse_vorbis_comment :: proc(arr: ^[]byte) -> (c: VorbisComment, err: ReadError) { 105 - fmt.printfln("Vorbis Comment Bytes: %d", len(arr)) 101 + app_core.log_debugf("%s %s", app_core.colorize("vorbis-bytes", app_core.ANSI_CYAN), app_core.colorize(fmt.tprintf("%d", len(arr)), app_core.ANSI_GREEN)) 106 102 107 103 length := u32(len(arr)) 108 104 cursor: u32 = 0 ··· 119 115 if (!ok) { 120 116 return VorbisComment{}, .UnableToReadVendorString 121 117 } 122 - fmt.printfln("Vendor string length %d", vendor_str_len) 118 + app_core.log_debugf("%s %s", app_core.colorize("vendor-length", app_core.ANSI_CYAN), app_core.colorize(fmt.tprintf("%d", vendor_str_len), app_core.ANSI_GREEN)) 123 119 cursor += 4 124 120 125 121 if (vendor_str_len > MAX_FIELD_LENGTH || cursor + vendor_str_len > length) { ··· 128 124 129 125 // Read vendor string 130 126 vendor_str := string(arr[cursor:cursor + vendor_str_len]) 131 - fmt.printfln("Vendor string %s", vendor_str) 127 + app_core.log_debugf("%s %s", app_core.colorize("vendor", app_core.ANSI_CYAN), app_core.colorize(vendor_str, app_core.ANSI_GREEN)) 132 128 cursor += vendor_str_len 133 129 134 130 //Read number of fields ··· 139 135 if (!ok) { 140 136 return VorbisComment{}, .UnknownError 141 137 } 142 - fmt.printfln("Number of fields %d", num_fields) 138 + app_core.log_debugf("%s %s", app_core.colorize("fields", app_core.ANSI_CYAN), app_core.colorize(fmt.tprintf("%d", num_fields), app_core.ANSI_GREEN)) 143 139 if (num_fields > MAX_VORBIS_FIELDS) { 144 140 return VorbisComment{}, .UnknownError 145 141 } ··· 165 161 if (field_length > MAX_FIELD_LENGTH || cursor + field_length > length) { 166 162 return VorbisComment{}, .UnknownError 167 163 } 168 - fmt.printfln("Field length %d", field_length) 164 + app_core.log_debugf("%s %s", app_core.colorize("field-length", app_core.ANSI_DIM), app_core.colorize(fmt.tprintf("%d", field_length), app_core.ANSI_GREEN)) 169 165 170 166 cursor += 4 171 167 172 168 field := string(arr[cursor:cursor + field_length]) 173 - fmt.printfln("field %v", field) 169 + app_core.log_debugf("%s %s", app_core.colorize("field", app_core.ANSI_YELLOW), app_core.colorize(field, app_core.ANSI_MAGENTA)) 174 170 175 171 pair := strings.split(field, "=") 176 172 defer delete(pair) ··· 180 176 key := pair[0] 181 177 value := pair[1] 182 178 183 - fmt.printfln("key |%v| value |%v| ", key, value) 179 + app_core.log_debugf("%s %s %s %s", app_core.colorize("key", app_core.ANSI_CYAN), app_core.colorize(key, app_core.ANSI_YELLOW), app_core.colorize("value", app_core.ANSI_CYAN), app_core.colorize(value, app_core.ANSI_GREEN)) 184 180 185 181 186 182 switch key { ··· 214 210 comment.album_artist = comment.artists[0] 215 211 } 216 212 217 - fmt.printfln("Comment %#v", comment) 213 + app_core.log_infof("%s %#v", app_core.colorize("parsed-comment", app_core.ANSI_CYAN), comment) 218 214 219 215 return comment, nil 220 216 } ··· 242 238 return VorbisComment{}, .UnknownError 243 239 } 244 240 245 - fmt.printfln("HeaderBytes: %v", headerBytes) 241 + app_core.log_debugf("%s %v", app_core.colorize("header-bytes", app_core.ANSI_DIM), headerBytes) 246 242 247 - fmt.printfln("About to call parse_header") 248 243 header := parse_header(headerBytes) 249 - fmt.printfln("parse_header returned successfully") 250 244 251 - fmt.printfln( 252 - "Parsed header: stream_info=%d, length=%d, is_last=%v", 253 - header.stream_info, 254 - header.length, 245 + app_core.log_debugf( 246 + "%s type=%s length=%s is_last=%v", 247 + app_core.colorize("header", app_core.ANSI_CYAN), 248 + app_core.colorize(fmt.tprintf("%d", header.stream_info), app_core.ANSI_YELLOW), 249 + app_core.colorize(fmt.tprintf("%d", header.length), app_core.ANSI_GREEN), 255 250 header.is_last, 256 251 ) 257 252 258 253 259 254 if (header.stream_info == VORBIS_COMMENT) { 260 - fmt.printfln("Found Vorbis Comment") 255 + app_core.log_infof("%s", app_core.colorize("found vorbis comment block", app_core.ANSI_CYAN)) 261 256 262 257 vorbisCommentBytes := make([]byte, header.length) 263 258 defer delete(vorbisCommentBytes) ··· 266 261 267 262 vn, verr := io.read_full(s, vorbisCommentBytes) 268 263 if verr != nil || cast(u32)vn != header.length { 269 - fmt.printfln("Failed to read voribs comment as bytes %v %d", verr, vn) 264 + app_core.log_errorf("failed reading vorbis comment bytes err=%v read=%d expected=%d", verr, vn, header.length) 270 265 return VorbisComment{}, .UnknownError 271 266 } 272 267 ··· 278 273 } 279 274 280 275 skip := header.length 281 - fmt.printfln("Skip: %i", skip) 276 + app_core.log_debugf("%s %s", app_core.colorize("skip", app_core.ANSI_DIM), app_core.colorize(fmt.tprintf("%d", skip), app_core.ANSI_GREEN)) 282 277 x, xerr := bufio.reader_discard(&r, int(skip)) 283 278 if xerr != nil { 284 - fmt.printfln("Discard error: %v", xerr) 279 + app_core.log_errorf("discard error: %v", xerr) 285 280 return VorbisComment{}, .UnknownError 286 281 } 287 282 if x != int(skip) { 288 - fmt.printfln("Discard incomplete: wanted %d, got %d", skip, x) 283 + app_core.log_errorf("discard incomplete: wanted %d, got %d", skip, x) 289 284 // Need to handle partial discard 290 285 } 291 286 292 - fmt.printfln("Discarded: %i", x) 287 + app_core.log_debugf("%s %d", app_core.colorize("discarded", app_core.ANSI_DIM), x) 293 288 294 289 } 295 290