1: /*
2: * Guitar-ZyX(tm)::MasterControlProgram - portable guitar F/X controller
3: * Copyright (C) 2009 Douglas McClendon
4: *
5: * This program is free software: you can redistribute it and/or modify
6: * it under the terms of the GNU General Public License as published by
7: * the Free Software Foundation, version 3 of the License.
8: *
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program. If not, see <http://www.gnu.org/licenses/>.
16: */
17: /*
18: #############################################################################
19: #############################################################################
20: ##
21: ## gzmcpc::debug: 'debugging' functions
22: ##
23: #############################################################################
24: ##
25: ## Copyright 2008-2009 Douglas McClendon <dmc AT filteredperception DOT org>
26: ##
27: #############################################################################
28: #############################################################################
29: #
30: */
31:
32:
33:
34:
35:
36: #include <stdio.h>
37: #include <stdlib.h>
38: #include <stdarg.h>
39: #include <string.h>
40: #include <dirent.h>
41: #include <sys/stat.h>
42: #include <unistd.h>
43: #include <malloc.h>
44:
45:
46: #include <fat.h>
47:
48:
49: #include "configfiles.h"
50:
51: #include "debug.h"
52:
53:
54:
55:
56: int retval = 0;
57:
58: int debug_tweak_var = 9;
59:
60: char debug_log_filename[256] = "off";
61: FILE *debug_log_file = 0;
62:
63: extern u8 __end__[];
64: extern u8 __eheap_end[];
65:
66:
67:
68:
69: void debug_log_init(const char *log_filename) {
70: // workaround involving opening and closing the file for each write
71: snprintf(debug_log_filename, 256, log_filename);
72:
73: // sigh, this did not seem to work, maybe fflush not implemented
74: // XXX exception handling
75: // debug_log_file = fopen(debug_log_filename, "a");
76: // note: this never really gets closed, depending on fflush to work
77: }
78:
79:
80: void debug_log(const char *debug_fmt, ...) {
81:
82: va_list debug_arg;
83:
84: char output_buffer[DBG_MSG_MAX_LEN];
85:
86: // only do work if necessary
87: if (!fs_avail) {
88: return;
89: }
90: if (strncmp(debug_log_filename, "off", 256) == 0) {
91: return;
92: }
93:
94: va_start(debug_arg, debug_fmt);
95: vsnprintf(output_buffer, DBG_MSG_MAX_LEN,
96: debug_fmt, debug_arg);
97: va_end(debug_arg);
98:
99: // write the message to the logfile
100: debug_log_file = fopen(debug_log_filename, "a");
101: fprintf(debug_log_file, "%s", output_buffer);
102: // was using this prior to workaround of open/close for every write
103: // fflush(debug_log_file);
104: fclose(debug_log_file);
105: }
106:
107:
108:
109: u8* get_heap_start() {
110: return __end__;
111: }
112: u8* get_heap_end() {
113: return (u8*)sbrk(0);
114: }
115: u8* get_heap_limit() {
116: return __eheap_end;
117: }
118:
119: int get_mem_used() {
120: struct mallinfo info = mallinfo();
121: return info.uordblks;
122: }
123:
124: int get_mem_free() {
125: struct mallinfo info = mallinfo();
126: return info.fordblks + (get_heap_limit() - get_heap_end());
127: }
128:
129:
130: void die(void) {
131: while (1);
132: }
133: