Skip to content

Commit 199a027

Browse files
authored
adding gcp log compatibility and new status codes (#26)
1 parent cbcca62 commit 199a027

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
build
44
build/***
55
core-*
6-

mos.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ config_schema:
3535
- ["mqtt.max_queue_length", "i", 5, {title: "Maximum queue length for buffering QoS 1+ messages. 0 to disable queue."}]
3636
- ["mqtt.ws_enable", "b", false, {title: "Enable WebSocket encapsulation"}]
3737
- ["mqtt.ws_path", "s", "/mqtt", {title: "Path to use for WebSocket handshake"}]
38+
- ["mqtt.debug_use_log_level", "b", false, {title: "Use the cs_log_level enum instead of stdout/stderr flag"}]
3839
# Alternative MQTT configuration. If enabled, client will alternate between mqtt and mqtt1
3940
# when unable to connect.
4041
- ["mqtt1", "mqtt", {title: "Backup MQTT settings"}]

src/mgos_mqtt.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,28 @@ void mgos_mqtt_set_connect_fn(mgos_mqtt_connect_fn_t fn, void *fn_arg) {
5555
static void s_debug_write_cb(int ev, void *ev_data, void *userdata) {
5656
if (s_conn == NULL) return;
5757
struct mgos_debug_hook_arg *arg = (struct mgos_debug_hook_arg *) ev_data;
58-
const char *topic =
59-
(arg->fd == 1
60-
? mgos_sys_config_get_debug_stdout_topic()
61-
: arg->fd == 2 ? mgos_sys_config_get_debug_stderr_topic() : NULL);
58+
const char *topic = (arg->fd == 1 ? mgos_sys_config_get_debug_stdout_topic()
59+
: arg->fd == 2 ? mgos_sys_config_get_debug_stderr_topic()
60+
: NULL);
6261
if (topic != NULL &&
6362
mgos_mqtt_num_unsent_bytes() < MGOS_MQTT_LOG_PUSHBACK_THRESHOLD) {
6463
static uint32_t s_seq = 0;
6564
char *msg = arg->buf;
65+
66+
int log_level = arg->fd;
67+
68+
if (mgos_sys_config_get_mqtt_debug_use_log_level()) {
69+
log_level = (int) arg->level; // ie LL_INFO
70+
if (arg->fd == 2) { // stderr
71+
log_level = 0; // LL_ERROR
72+
}
73+
}
74+
6675
int msg_len = mg_asprintf(
6776
&msg, MGOS_DEBUG_TMP_BUF_SIZE, "%s %u %.3lf %d|%.*s",
6877
(mgos_sys_config_get_device_id() ? mgos_sys_config_get_device_id()
6978
: "-"),
70-
(unsigned int) s_seq, mg_time(), arg->fd, (int) arg->len,
79+
(unsigned int) s_seq, mg_time(), log_level, (int) arg->len,
7180
(const char *) arg->data);
7281
if (arg->len > 0) {
7382
mgos_mqtt_conn_pub(s_conn, topic, mg_mk_str_n(msg, msg_len), 0 /* qos */,

src/mgos_mqtt_conn.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "mgos.h"
2323
#include "mgos_debug.h"
24+
#include "mgos_event.h"
2425
#include "mgos_mongoose.h"
2526

2627
static void mgos_mqtt_conn_net_ev(int ev, void *evd, void *arg);
@@ -186,7 +187,13 @@ void mgos_mqtt_ev(struct mg_connection *nc, int ev, void *ev_data,
186187
LOG(LL_INFO, ("MQTT%d %s connected %s (%d)", c->conn_id,
187188
(cfg->ws_enable ? "WS" : "TCP"),
188189
(status == 0 ? "ok" : "error"), status));
189-
if (status != 0) break;
190+
if (status != 0) {
191+
if (cfg->cloud_events) {
192+
struct mgos_cloud_arg arg = {.type = MGOS_CLOUD_MQTT};
193+
mgos_event_trigger(MGOS_EVENT_CLOUD_CONNECTERROR, &arg);
194+
}
195+
break;
196+
}
190197
struct mg_send_mqtt_handshake_opts opts;
191198
memset(&opts, 0, sizeof(opts));
192199
opts.user_name = cfg->user;

0 commit comments

Comments
 (0)