Skip to content

Commit

Permalink
Display realtime Motionplus log on webcontrol
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Dave committed Aug 21, 2024
1 parent 7e9cf98 commit daf1ba9
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 5 deletions.
35 changes: 34 additions & 1 deletion src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,37 @@ void ff_log(void *var1, int errnbr, const char *fmt, va_list vlist)
}
}

void cls_log::log_history_init()
{
int indx;
ctx_log_item log_item;

log_item.log_msg= "";
for (indx=0;indx<200;indx++){
log_item.log_nbr = indx;
log_vec.push_back(log_item);
}
}

void cls_log::log_history_add(std::string msg)
{
int indx, mx;

mx = (int)log_vec.size();
for (indx=0;indx<mx-1;indx++) {
log_vec[indx].log_nbr = log_vec[indx+1].log_nbr;
log_vec[indx].log_msg = log_vec[indx+1].log_msg;
}

/*Arbritrary large number*/
if (log_vec[mx-1].log_nbr >50000000L) {
log_history_init();
}
log_vec[mx-1].log_nbr++;
log_vec[mx-1].log_msg = msg;

}

void cls_log::write_flood(int loglvl)
{
char flood_repeats[1024];
Expand All @@ -79,6 +110,7 @@ void cls_log::write_flood(int loglvl)
fputs(flood_repeats, stderr);
fflush(stderr);
}
log_history_add(flood_repeats);
}

void cls_log::write_norm(int loglvl, uint prefixlen)
Expand All @@ -102,6 +134,7 @@ void cls_log::write_norm(int loglvl, uint prefixlen)
fputs(msg_full, stderr);
fflush(stderr);
}
log_history_add(msg_full);
}

void cls_log::add_errmsg(int flgerr, int err_save)
Expand Down Expand Up @@ -273,7 +306,7 @@ cls_log::cls_log(cls_motapp *p_app)
memset(msg_prefix,0,sizeof(msg_prefix));
memset(msg_flood,0,sizeof(msg_flood));
memset(msg_full,0,sizeof(msg_full));

log_history_init();
av_log_set_callback(ff_log);
}

Expand Down
10 changes: 9 additions & 1 deletion src/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
#define MOTPLS_LOG(x, y, z, ...) motlog->write_msg(x, y, z, 1, __FUNCTION__, __VA_ARGS__)
#define MOTPLS_SHT(x, y, z, ...) motlog->write_msg(x, y, z, 0, __VA_ARGS__)

struct ctx_log_item {
uint64_t log_nbr;
std::string log_msg;
};

class cls_log {
public:
cls_log(cls_motapp *p_app);
Expand All @@ -66,6 +71,7 @@
void shutdown();
void startup();
bool restart;
std::vector<ctx_log_item> log_vec;
private:
cls_motapp *app;
int log_mode;
Expand All @@ -75,11 +81,13 @@
char msg_flood[1024];
char msg_full[1024];
int flood_cnt;

void set_mode(int mode);
void write_flood(int loglvl);
void write_norm(int loglvl, uint prefixlen);
void add_errmsg(int flgerr, int err_save);
void log_stop();
void log_history_init();
void log_history_add(std::string msg);

};

Expand Down
3 changes: 3 additions & 0 deletions src/motionplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ void cls_motapp::check_restart()
pid_remove();
}

MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Restarting log"));
pthread_mutex_lock(&motlog->mutex_log);
motlog->shutdown();
cfg->parms_copy(conf_src, PARM_CAT_00);
Expand All @@ -570,6 +571,7 @@ void cls_motapp::check_restart()
}

if (dbse->restart == true) {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Restarting database"));
pthread_mutex_lock(&dbse->mutex_dbse);
dbse->shutdown();
cfg->parms_copy(conf_src, PARM_CAT_15);
Expand All @@ -579,6 +581,7 @@ void cls_motapp::check_restart()
}

if (webu->restart == true) {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Restarting webcontrol"));
webu->shutdown();
cfg->parms_copy(conf_src, PARM_CAT_13);
webu->startup();
Expand Down
2 changes: 1 addition & 1 deletion src/webu_ans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void cls_webu_ans::answer_get()
}
webu_file->main();

} else if ((uri_cmd1 == "config.json") ||
} else if ((uri_cmd1 == "config.json") || (uri_cmd1 == "log") ||
(uri_cmd1 == "movies.json") || (uri_cmd1 == "status.json")) {
if (webu_json == nullptr) {
webu_json = new cls_webu_json(this);
Expand Down
96 changes: 95 additions & 1 deletion src/webu_html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,27 @@ void cls_webu_html::style_config()
" color: black;\n"
" background-color: transparent;\n"
" text-decoration: none;\n"
" }\n";
" }\n"

" .cls_log {\n"
" background-color: transparent;\n"
" color: white;\n"
" text-align: center;\n"
" margin-top: 0rem;\n"
" margin-bottom: 0rem;\n"
" font-weight: normal;\n"
" font-size: 0.90rem;\n"
" }\n"

" .cls_log textarea {\n"
" overflow-y: scroll;\n"
" background-color: lightgrey;\n"
" padding: 0rem;\n"
" height: 10rem;\n"
" width: 50rem;\n"
" text-align: left;\n"
" }\n";


}

Expand Down Expand Up @@ -359,7 +379,11 @@ void cls_webu_html::divmain()
" <div id='div_movies' class='cls_movies'>\n"
" <!-- Filled in by script -->\n"
" </div>\n\n"
" <div id='div_log' class='cls_log' style='display:none' >\n"
" <textarea id='txta_log' ></textarea>\n"
" </div>\n\n"
" </div>\n\n";

}

/* Create the javascript function send_config */
Expand Down Expand Up @@ -717,6 +741,10 @@ void cls_webu_html::script_assign_actions()
}
}

webua->resp_page +=
" html_actions += \"<a onclick=\\\"log_showhide();\\\">\";\n"
" html_actions += \"Show/hide log</a>\\n\";\n\n";

webua->resp_page +=
" document.getElementById(\"divnav_actions\").innerHTML = html_actions;\n\n"
" return;\n"
Expand Down Expand Up @@ -1077,6 +1105,7 @@ void cls_webu_html::script_cams_reset()
" document.getElementById('pic'+indx).src = ''; \n"
" }\n"
" }\n"
" document.getElementById('cfgpic').src = ''; \n"
" } \n\n";
}

Expand Down Expand Up @@ -1382,13 +1411,74 @@ void cls_webu_html::script_cams_scan_fnc()
" };\n\n";
}

void cls_webu_html::script_log_display()
{
webua->resp_page +=
" function log_display() {\n"
" var itm, msg, nbr, indx, txtalog;\n"
" txtalog = document.getElementById('txta_log').value;\n"
" for (indx = 0; indx < 1000; indx++) {\n"
" itm = pLog[indx];\n"
" if (typeof(itm) != 'undefined') {\n"
" msg = pLog[indx]['logmsg'];\n"
" nbr = pLog[indx]['lognbr'];\n"
" if (txtalog.length > 1000) {\n"
" txtalog = txtalog.substring(txtalog.search('\\n'));\n"
" }\n"
" txtalog += '\\n' + msg;\n"
" }\n"
" }\n"
" document.getElementById('txta_log').enabled = true;\n"
" document.getElementById('txta_log').value = txtalog;\n"
" document.getElementById('txta_log').scrollTop =\n"
" document.getElementById('txta_log').scrollHeight;\n"
" document.getElementById('txta_log').enabled = false;\n"
" }\n\n";

}

void cls_webu_html::script_log_get()
{
webua->resp_page +=
" function log_get() {\n"
" var xmlhttp = new XMLHttpRequest();\n"
" xmlhttp.onreadystatechange = function() {\n"
" if (this.readyState == 4 && this.status == 200) {\n"
" pLog = JSON.parse(this.responseText);\n"
" log_display();\n"
" }\n"
" };\n"
" xmlhttp.open('GET', pHostFull+'/0/log/0');\n"
" xmlhttp.send();\n"
" }\n\n";

}

void cls_webu_html::script_log_showhide()
{
webua->resp_page +=
" function log_showhide() {\n"
" if (document.getElementById('div_log').style.display == 'none') {\n"
" document.getElementById('div_log').style.display='block';\n"
" document.getElementById('txta_log').value = '';\n"
" log_timer = setInterval(log_get, 2000);\n"
" } else {\n"
" document.getElementById('div_log').style.display='none';\n"
" document.getElementById('txta_log').value = '';\n"
" clearInterval(log_timer);\n"
" }\n"
" }\n\n";

}

/* Call all the functions to create the java scripts of page*/
void cls_webu_html::script()
{
webua->resp_page += " <script>\n"
" var pData, pMovies, pHostFull;\n"
" var gIndxScan, gIndxCam, gGetImgs;\n"
" var pic_url = Array(4);\n"
" var log_timer;\n"
" var cams_scan_timer, cams_all_timer, cams_one_timer;\n\n";

script_nav();
Expand Down Expand Up @@ -1433,6 +1523,10 @@ void cls_webu_html::script()
script_movies_page();
script_movies_click();

script_log_display();
script_log_get();
script_log_showhide();

webua->resp_page += " </script>\n\n";
}

Expand Down
3 changes: 3 additions & 0 deletions src/webu_html.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
void script_cams_one_fnc();
void script_cams_all_fnc();
void script_cams_scan_fnc();
void script_log_display();
void script_log_get();
void script_log_showhide();
void script();
void body();
void default_page();
Expand Down
40 changes: 39 additions & 1 deletion src/webu_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,41 @@ void cls_webu_json::status()
webua->resp_page += "}";
}

void cls_webu_json::loghistory()
{
int indx, cnt;
bool frst;

webua->resp_type = WEBUI_RESP_JSON;

pthread_mutex_lock(&motlog->mutex_log);
frst = true;
cnt = 0;
for (indx=0; indx<motlog->log_vec.size();indx++) {
if (motlog->log_vec[indx].log_nbr > mtoi(webua->uri_cmd2)) {
if (frst == true) {
webua->resp_page += "{";
frst = false;
} else {
webua->resp_page += ",";
}
webua->resp_page += "\"" + std::to_string(indx) +"\" : {";
webua->resp_page += "\"lognbr\" :\"" +
std::to_string(motlog->log_vec[indx].log_nbr) + "\", ";
webua->resp_page += "\"logmsg\" :\"" +
motlog->log_vec[indx].log_msg.substr(0,
motlog->log_vec[indx].log_msg.length()-1) + "\" ";
webua->resp_page += "}";
cnt++;
}
}
pthread_mutex_unlock(&motlog->mutex_log);
if (frst == true) {
webua->resp_page += "{\"0\": \"\" ";
}
webua->resp_page += ",\"count\":\""+std::to_string(cnt)+"\"}";
}

void cls_webu_json::main()
{
pthread_mutex_lock(&app->mutex_post);
Expand All @@ -428,7 +463,10 @@ void cls_webu_json::main()
} else if (webua->uri_cmd1 == "movies.json") {
movies();
} else if (webua->uri_cmd1 == "status.json") {
status();
//status();
loghistory();
} else if (webua->uri_cmd1 == "log") {
loghistory();
} else {
webua->bad_request();
pthread_mutex_unlock(&app->mutex_post);
Expand Down
1 change: 1 addition & 0 deletions src/webu_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
void movies();
void status_vars(int indx_cam);
void status();
void loghistory();
};

#endif /* _INCLUDE_WEBU_JSON_HPP_ */

0 comments on commit daf1ba9

Please sign in to comment.