Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow control over host debug messages #2289

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Components/driver/hw_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void* CTimerThread::thread_routine()
continue; // state changed
}
if(errno != ETIMEDOUT) {
hostmsg("Warning! Timer thread errno = %u", errno);
host_debug_w("Timer thread errno = %u", errno);
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions Sming/Arch/Host/Components/driver/uart_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void CUartServer::terminate()
{
close();
join();
hostmsg("UART%u server destroyed", uart_nr);
host_debug_i("UART%u server destroyed", uart_nr);
}

void CUartServer::onNotify(smg_uart_t* uart, smg_uart_notify_code_t code)
Expand Down Expand Up @@ -271,7 +271,7 @@ int CUartServer::serviceWrite()
do {
int sent = socket->send(data, avail);
if(sent < 0) {
hostmsg("Uart send returned %d", sent);
host_debug_w("Uart send returned %d", sent);
result = sent;
break;
}
Expand All @@ -295,11 +295,11 @@ void* CUartServer::thread_routine()
auto port = portBase + uart_nr;
CSockAddr addr(nullptr, port);
if(!listen(addr, 1)) {
hostmsg("Listen %s failed", addr.text().c_str());
host_debug_e("Listen %s failed", addr.text().c_str());
return nullptr;
}

hostmsg("UART%u server listening on port %u", uart_nr, port);
host_debug_i("UART%u server listening on port %u", uart_nr, port);

while(active()) {
socket = try_connect();
Expand All @@ -308,7 +308,7 @@ void* CUartServer::thread_routine()
continue;
}

hostmsg("Uart #%u socket open", uart_nr);
host_debug_i("Uart #%u socket open", uart_nr);

while(socket->active()) {
if(txsem.timedwait(IDLE_SLEEP_MS)) {
Expand All @@ -335,7 +335,7 @@ void* CUartServer::thread_routine()
}

socket->close();
hostmsg("Uart #%u socket closed", uart_nr);
host_debug_i("Uart #%u socket closed", uart_nr);
}

return nullptr;
Expand Down
8 changes: 4 additions & 4 deletions Sming/Arch/Host/Components/esp_hal/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ const uint8_t HOST_TASK_PRIO = USER_TASK_PRIO_MAX;
bool system_os_task(os_task_t callback, uint8_t prio, os_event_t* events, uint8_t qlen)
{
if(prio >= USER_TASK_PRIO_MAX) {
hostmsg("Invalid priority %u", prio);
host_debug_e("Invalid priority %u", prio);
return false;
}
auto& queue = task_queues[prio];
if(queue != nullptr) {
hostmsg("Queue %u already initialised", prio);
host_debug_e("Queue %u already initialised", prio);
return false;
}

Expand All @@ -71,12 +71,12 @@ bool system_os_task(os_task_t callback, uint8_t prio, os_event_t* events, uint8_
bool system_os_post(uint8_t prio, os_signal_t sig, os_param_t par)
{
if(prio >= USER_TASK_PRIO_MAX) {
hostmsg("Invalid priority %u", prio);
host_debug_e("Invalid priority %u", prio);
return false;
}
auto& queue = task_queues[prio];
if(queue == nullptr) {
hostmsg("Task queue %u not initialised", prio);
host_debug_e("Task queue %u not initialised", prio);
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions Sming/Arch/Host/Components/hostlib/hostmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <string.h>
#include "hostmsg.h"

int host_debug_level = 2;

/*
* e.g. from "void a::sub(int)" we just want "a::sub"
*/
Expand Down
16 changes: 16 additions & 0 deletions Sming/Arch/Host/Components/hostlib/hostmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ void host_puts(const char* str);
#define hostmsg(fmt, ...) host_printfp(fmt "\n", __func__, ##__VA_ARGS__)
#endif

/**
* @brief Emit message only if host_debug_level >= level
*/
#define host_debug(level, fmt, ...) \
do { \
if(host_debug_level >= (level)) { \
host_printf(fmt "\n", ##__VA_ARGS__); \
} \
} while(0)

#define host_debug_e(fmt, ...) host_debug(0, "Error! " fmt, ##__VA_ARGS__)
#define host_debug_w(fmt, ...) host_debug(1, "Warning! " fmt, ##__VA_ARGS__)
#define host_debug_i(fmt, ...) host_debug(2, fmt, ##__VA_ARGS__)

extern int host_debug_level;

#ifdef __cplusplus
}
#endif
4 changes: 3 additions & 1 deletion Sming/Arch/Host/Components/hostlib/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
XX(flashsize, required_argument, "Change default flash size if file doesn't exist", "SIZE", \
"Size of flash in bytes (e.g. 512K, 524288, 0x80000)", nullptr) \
XX(initonly, no_argument, "Initialise only, do not start Sming", nullptr, nullptr, nullptr) \
XX(nonet, no_argument, "Skip network initialisation", nullptr, nullptr, nullptr)
XX(nonet, no_argument, "Skip network initialisation", nullptr, nullptr, nullptr) \
XX(debug, required_argument, "Set debug verbosity", "LEVEL", "Maximum debug message level to print", \
"0 = errors only, 1 = +warnings, 2 = +info\0")

enum option_tag_t {
#define XX(tag, has_arg, desc, argname, arghelp, examples) opt_##tag,
Expand Down
10 changes: 5 additions & 5 deletions Sming/Arch/Host/Components/hostlib/sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,20 @@ bool CSocket::create()
// creation of the socket
m_fd = ::socket(AF_INET, m_type, 0);
if(m_fd <= 0) {
hostmsg("%s", socket_strerror().c_str());
host_debug_e("%s", socket_strerror().c_str());
return false;
}

int reuse = 1;
if(setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (sock_ptr_t)&reuse, sizeof(reuse)) < 0) {
hostmsg("REUSEADDR: %s", socket_strerror().c_str());
host_debug_e("REUSEADDR: %s", socket_strerror().c_str());
close();
return false;
}

#ifdef SO_REUSEPORT
if(setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (sock_ptr_t)&reuse, sizeof(reuse)) < 0) {
hostmsg("REUSEPORT: %s", socket_strerror().c_str());
host_debug_e("REUSEPORT: %s", socket_strerror().c_str());
close();
return false;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ void CSocket::close()
return;
}

hostmsg("%s", addr().text().c_str());
host_debug_i("%s", addr().text().c_str());

socket_close(m_fd);
m_fd = 0;
Expand Down Expand Up @@ -375,7 +375,7 @@ CSocket* CSocketList::recv(void* buf, size_t& n)
#endif
if(errno != EPIPE) {
// Broken pipe
hostmsg("%s", socket_strerror().c_str());
host_debug_e("%s", socket_strerror().c_str());
}
skt->close();
}
Expand Down
28 changes: 17 additions & 11 deletions Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ static void cleanup()
CUartServer::shutdown();
sockets_finalise();
host_lwip_shutdown();
hostmsg("Goodbye!");
host_debug_i("Goodbye!");
}

void host_exit(int code)
{
static unsigned exit_count;

hostmsg("returning %d", code);
host_debug_i("returning %d", code);
exitCode = code;
done = true;

if(exit_count++) {
hostmsg("Forcing exit");
host_debug_w("Forcing exit");
exit(exitCode);
}
}
Expand Down Expand Up @@ -108,8 +108,6 @@ int main(int argc, char* argv[])
{
trap_exceptions();

host_printf("\nWelcome to the Sming Host emulator\n\n");

static struct {
int pause;
int exitpause;
Expand Down Expand Up @@ -197,10 +195,16 @@ int main(int argc, char* argv[])
config.enable_network = false;
break;

case opt_debug:
host_debug_level = atoi(arg);
break;

default:;
}
}

host_debug_i("\nWelcome to the Sming Host emulator\n\n");

auto i = get_first_non_option();
commandLine.parse(argc - i, &argv[i]);

Expand All @@ -213,7 +217,7 @@ int main(int argc, char* argv[])
atexit(cleanup);

if(config.initonly) {
hostmsg("Initialise-only requested");
host_debug_i("Initialise-only requested");
} else {
Storage::initialize();

Expand All @@ -233,13 +237,15 @@ int main(int argc, char* argv[])
host_wifi_lwip_init_complete();
}
} else {
hostmsg("Network initialisation skipped as requested");
host_debug_i("Network initialisation skipped as requested");
}

hostmsg("If required, you may start terminal application(s) now");
pause(config.pause);
if(config.pause != 0) {
hostmsg("If required, you may start terminal application(s) now");
pause(config.pause);
}

hostmsg(">> Starting Sming <<\n");
host_debug_i(">> Starting Sming <<\n");

System.initialize();

Expand All @@ -257,7 +263,7 @@ int main(int argc, char* argv[])
system_soft_wdt_feed();
}

hostmsg(">> Normal Exit <<\n");
host_debug_i(">> Normal Exit <<\n");
}

pause(config.exitpause);
Expand Down
21 changes: 11 additions & 10 deletions Sming/Arch/Host/Components/lwip/Linux/host_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static bool getifaddr(const char* ifname, struct net_config* netcfg)
{
struct ifaddrs* list;
if(getifaddrs(&list) < 0) {
hostmsg("getifaddrs: %s", strerror(errno));
host_debug_e("getifaddrs: %s", strerror(errno));
return false;
}

Expand Down Expand Up @@ -111,26 +111,26 @@ static bool getifaddr(const char* ifname, struct net_config* netcfg)

bool host_lwip_init(const struct lwip_param* param)
{
hostmsg("%s", "Initialising LWIP");
host_debug_i("%s", "Initialising LWIP");

struct net_config netcfg = {0};

if(!getifaddr(param->ifname, &netcfg)) {
if(param->ifname == NULL) {
hostmsg("%s", "No compatible interface found");
host_debug_e("%s", "No compatible interface found");
} else {
hostmsg("Interface '%s' not found", param->ifname);
host_debug_e("Interface '%s' not found", param->ifname);
}
return false;
}

if(param->gateway != NULL && ip4addr_aton(param->gateway, &netcfg.gw) != 1) {
hostmsg("Failed to parse provided Gateway address '%s'", param->gateway);
host_debug_e("Failed to parse provided Gateway address '%s'", param->gateway);
return false;
}

if(param->netmask != NULL && ip4addr_aton(param->netmask, &netcfg.netmask) != 1) {
hostmsg("Failed to parse provided Network Mask '%s'", param->netmask);
host_debug_e("Failed to parse provided Network Mask '%s'", param->netmask);
return false;
}

Expand All @@ -139,7 +139,7 @@ bool host_lwip_init(const struct lwip_param* param)
IP4_ADDR(&netcfg.ipaddr, (uint32_t)ip4_addr1(&netcfg.gw), (uint32_t)ip4_addr2(&netcfg.gw),
(uint32_t)ip4_addr3(&netcfg.gw), 10U);
} else if(ip4addr_aton(param->ipaddr, &netcfg.ipaddr) != 1) {
hostmsg("Failed to parse provided IP address '%s'", param->ipaddr);
host_debug_e("Failed to parse provided IP address '%s'", param->ipaddr);
return false;
}

Expand All @@ -149,7 +149,8 @@ bool host_lwip_init(const struct lwip_param* param)
ip4addr_ntoa_r(&netcfg.netmask, nm_str, sizeof(nm_str));
char gw_str[IP4ADDR_STRLEN_MAX];
ip4addr_ntoa_r(&netcfg.gw, gw_str, sizeof(gw_str));
hostmsg("Using interface '%s', gateway = %s, netmask = %s; using ip = %s", netcfg.ifname, gw_str, nm_str, ip_str);
host_debug_i("Using interface '%s', gateway = %s, netmask = %s; using ip = %s", netcfg.ifname, gw_str, nm_str,
ip_str);

setenv("PRECONFIGURED_TAPIF", netcfg.ifname, true);

Expand All @@ -158,8 +159,8 @@ bool host_lwip_init(const struct lwip_param* param)
netif_add(&netif, &netcfg.ipaddr, &netcfg.netmask, &netcfg.gw, NULL, tapif_init, ethernet_input);

getMacAddress(netcfg.ifname, netif.hwaddr);
hostmsg("MAC: %02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1], netif.hwaddr[2], netif.hwaddr[3],
netif.hwaddr[4], netif.hwaddr[5]);
host_debug_i("MAC: %02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1], netif.hwaddr[2],
netif.hwaddr[3], netif.hwaddr[4], netif.hwaddr[5]);

netif_set_default(&netif);

Expand Down
16 changes: 8 additions & 8 deletions Sming/Arch/Host/Components/lwip/Windows/host_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static bool find_adapter(const struct lwip_param* param, struct net_config* netc
pcap_if_t* alldevs;
char errbuf[PCAP_ERRBUF_SIZE + 1];
if(pcap_findalldevs(&alldevs, errbuf) < 0) {
hostmsg("Error in pcap_findalldevs: %s", errbuf);
host_debug_e("Error in pcap_findalldevs: %s", errbuf);
return false;
}

Expand Down Expand Up @@ -163,7 +163,7 @@ static bool find_adapter(const struct lwip_param* param, struct net_config* netc

bool host_lwip_init(const struct lwip_param* param)
{
hostmsg("%s", "Initialising LWIP");
host_debug_i("%s", "Initialising LWIP");

if(!npcap_init()) {
return false;
Expand All @@ -172,17 +172,17 @@ bool host_lwip_init(const struct lwip_param* param)
struct net_config netcfg = {0};

if(param->ipaddr != NULL && ip4addr_aton(param->ipaddr, &netcfg.ipaddr) != 1) {
hostmsg("Failed to parse IP address '%s'", param->ipaddr);
host_debug_e("Failed to parse IP address '%s'", param->ipaddr);
return false;
}

if(param->netmask != NULL && ip4addr_aton(param->netmask, &netcfg.netmask) != 1) {
hostmsg("Failed to parse Network Mask '%s'", param->netmask);
host_debug_e("Failed to parse Network Mask '%s'", param->netmask);
return false;
}

if(param->gateway != NULL && ip4addr_aton(param->gateway, &netcfg.gw) != 1) {
hostmsg("Failed to parse Gateway address '%s'", param->gateway);
host_debug_e("Failed to parse Gateway address '%s'", param->gateway);
return false;
}

Expand All @@ -196,7 +196,7 @@ bool host_lwip_init(const struct lwip_param* param)
ip4addr_ntoa_r(&netcfg.netmask, nm_str, sizeof(nm_str));
char gw_str[IP4ADDR_STRLEN_MAX];
ip4addr_ntoa_r(&netcfg.gw, gw_str, sizeof(gw_str));
hostmsg("gateway = %s, netmask = %s; using ip = %s", gw_str, nm_str, ip_str);
host_debug_i("gateway = %s, netmask = %s; using ip = %s", gw_str, nm_str, ip_str);

// Even though we're running as NO_SYS, stuff like crypt needs initialising
sys_init();
Expand All @@ -206,8 +206,8 @@ bool host_lwip_init(const struct lwip_param* param)
netif_add(&netif, &netcfg.ipaddr, &netcfg.netmask, &netcfg.gw, state, pcapif_init, ethernet_input);
netif_set_default(&netif);

hostmsg("MAC: %02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1], netif.hwaddr[2], netif.hwaddr[3],
netif.hwaddr[4], netif.hwaddr[5]);
host_debug_i("MAC: %02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1], netif.hwaddr[2],
netif.hwaddr[3], netif.hwaddr[4], netif.hwaddr[5]);

return true;
}
Expand Down
Loading