91 lines
2.4 KiB
C
91 lines
2.4 KiB
C
/***************************************************************************************
|
|
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
|
*
|
|
* NEMU is licensed under Mulan PSL v2.
|
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
* You may obtain a copy of Mulan PSL v2 at:
|
|
* http://license.coscl.org.cn/MulanPSL2
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
*
|
|
* See the Mulan PSL v2 for more details.
|
|
***************************************************************************************/
|
|
// Modified by: Zhiyi Zhang in 2024.08
|
|
|
|
#ifndef __DEBUG_H__
|
|
#define __DEBUG_H__
|
|
|
|
#include <stdio.h>
|
|
#include <assert.h>
|
|
|
|
#ifdef __DEBUG_LEVEL__
|
|
#undef __DEBUG_LEVEL__
|
|
#endif
|
|
/* You can change the debug level here
|
|
* 0: no log
|
|
* 1: Assert
|
|
* 2: Assert + Log
|
|
*/
|
|
#define __DEBUG_LEVEL__ 9
|
|
|
|
// ----------- log -----------
|
|
|
|
#define ANSI_FG_BLACK "\33[1;30m"
|
|
#define ANSI_FG_RED "\33[1;31m"
|
|
#define ANSI_FG_GREEN "\33[1;32m"
|
|
#define ANSI_FG_YELLOW "\33[1;33m"
|
|
#define ANSI_FG_BLUE "\33[1;34m"
|
|
#define ANSI_FG_MAGENTA "\33[1;35m"
|
|
#define ANSI_FG_CYAN "\33[1;36m"
|
|
#define ANSI_FG_WHITE "\33[1;37m"
|
|
#define ANSI_BG_BLACK "\33[1;40m"
|
|
#define ANSI_BG_RED "\33[1;41m"
|
|
#define ANSI_BG_GREEN "\33[1;42m"
|
|
#define ANSI_BG_YELLOW "\33[1;43m"
|
|
#define ANSI_BG_BLUE "\33[1;44m"
|
|
#define ANSI_BG_MAGENTA "\33[1;35m"
|
|
#define ANSI_BG_CYAN "\33[1;46m"
|
|
#define ANSI_BG_WHITE "\33[1;47m"
|
|
#define ANSI_NONE "\33[0m"
|
|
|
|
// Maybe Some Terminal Doesn't Support Color
|
|
#ifndef ANSI_FMT_DISABLE
|
|
#define ANSI_FMT(str, fmt) fmt str ANSI_NONE
|
|
#else
|
|
#define ANSI_FMT(str, fmt) str
|
|
#endif
|
|
|
|
#define _Log(...) \
|
|
do { \
|
|
printf(__VA_ARGS__); \
|
|
} while (0)
|
|
|
|
#if __DEBUG_LEVEL__ >= 2
|
|
#define Log(format, ...) \
|
|
_Log(ANSI_FMT("[%s:%d %s] " format, ANSI_FG_BLUE) "\n", \
|
|
__FILE__, __LINE__, __func__, ## __VA_ARGS__)
|
|
#else
|
|
#define Log(format, ...) (NULL)
|
|
#endif
|
|
|
|
#if __DEBUG_LEVEL__ >= 1
|
|
#define Assert(cond, format, ...) \
|
|
do { \
|
|
if (!(cond)) { \
|
|
fflush(stdout); \
|
|
fprintf(stderr, ANSI_FMT(format, ANSI_FG_RED) "\n", ## __VA_ARGS__); \
|
|
assert(cond); \
|
|
} \
|
|
} while (0)
|
|
#else
|
|
#define Assert(cond, format, ...) (NULL)
|
|
#endif
|
|
|
|
#define panic(format, ...) Assert(0, format, ## __VA_ARGS__)
|
|
|
|
#define TODO() panic("please implement me")
|
|
|
|
#endif
|