Skip to content

Commit

Permalink
Refactor print output
Browse files Browse the repository at this point in the history
SysModFiles: remove prints
SysModModel:
- setValue: only print if not ro
- pre post varDetails: use printVar

SysModPins: remove prints

SysModPrint:
- rename print to printf
- refactor printVar (id:value), recursive
- add prf (wip)
- comment print function/not/async

SysModUI:
- remove prints
- callVarFun: add prints

SysModWeb:
- remove prints, use prf
- sendResponseObject: condensed print
  • Loading branch information
ewoudwijma committed Apr 18, 2024
1 parent c8a56d1 commit 956cd9f
Show file tree
Hide file tree
Showing 11 changed files with 704 additions and 1,405 deletions.
3 changes: 1 addition & 2 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ void SysModFiles::setup() {
return true;
case f_AddRow:
rowNr = fileList.size();
USER_PRINTF("chFun addRow %s[%d] = %s\n", mdl->varID(var), rowNr, var["value"].as<String>().c_str());
web->getResponseObject()["addRow"]["rowNr"] = rowNr;
//add a row with all defaults
return true;
Expand Down Expand Up @@ -197,7 +196,7 @@ bool SysModFiles::seqNrToName(char * fileName, size_t seqNr) {
size_t counter = 0;
while (file) {
if (counter == seqNr) {
USER_PRINTF("seqNrToName: %s %d\n", file.name(), file.size());
// USER_PRINTF("seqNrToName: %d %s %d\n", seqNr, file.name(), file.size());
root.close();
strncat(fileName, "/", 31); //add root prefix, fileName is 32 bytes but sizeof doesn't know so cheating
strncat(fileName, file.name(), 31);
Expand Down
38 changes: 22 additions & 16 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ class SysModModel:public SysModule {
USER_PRINTF("dev setValue value removed %s %s\n", varID(var), var["oldValue"].as<String>().c_str());
}
else {
if (varRO(var))
USER_PRINTF("setValue changed %s %s\n", varID(var), var["value"].as<String>().c_str());
else
//only print if ! read only
if (!varRO(var))
USER_PRINTF("setValue changed %s %s -> %s\n", varID(var), var["oldValue"].as<String>().c_str(), var["value"].as<String>().c_str());
// else
// USER_PRINTF("setValue changed %s %s\n", varID(var), var["value"].as<String>().c_str());
web->addResponse(var["id"], "value", var["value"]);
changed = true;
}
Expand All @@ -233,8 +234,7 @@ class SysModModel:public SysModule {
//if we deal with multiple rows, value should be an array, if not we create one

if (var["value"].isNull() || !var["value"].is<JsonArray>()) {
USER_PRINTF("setValue var %s[%d] value %s not array, creating\n", varID(var), rowNr, var["value"].as<String>().c_str());
// print->printJson("setValueB var %s value %s not array, creating", id, var["value"].as<String>().c_str());
// USER_PRINTF("setValue var %s[%d] value %s not array, creating\n", varID(var), rowNr, var["value"].as<String>().c_str());
var["value"].to<JsonArray>();
}

Expand Down Expand Up @@ -375,29 +375,33 @@ class SysModModel:public SysModule {
}
}
setValueRowNr = rowNr;
print->printJson("varPreDetails post", var);
USER_PRINTF("varPreDetails post ");
print->printVar(var);
USER_PRINTF("\n");
}

void varPostDetails(JsonObject var, unsigned8 rowNr) {

setValueRowNr = UINT8_MAX;
if (rowNr != UINT8_MAX) {

print->printJson("varPostDetails pre", var);
USER_PRINTF("varPostDetails pre ");
print->printVar(var);
USER_PRINTF("\n");

//check if post init added: parent is already >=0
if (varOrder(var) >= 0) {
for (JsonArray::iterator childVar=varChildren(var).begin(); childVar!=varChildren(var).end(); ++childVar) { //use iterator to make .remove work!!!
JsonArray valArray = varValArray(*childVar);
// for (JsonArray::iterator childVar=varChildren(var).begin(); childVar!=varChildren(var).end(); ++childVar) { //use iterator to make .remove work!!!
for (JsonObject childVar: varChildren(var)) { //use iterator to make .remove work!!!
JsonArray valArray = varValArray(childVar);
if (!valArray.isNull())
{

if (varOrder(*childVar) < 0) { //if not updated
if (varOrder(childVar) < 0) { //if not updated
valArray[rowNr] = (char*)0; // set element in valArray to 0

USER_PRINTF("varPostDetails %s[%d] to null\n", varID(var), rowNr);
USER_PRINTF("varPostDetails %s.%s[%d] <- null\n", varID(var), varID(childVar), rowNr);
// setValue(var, -99, rowNr); //set value -99
varOrder(*childVar, -varOrder(*childVar)); //make positive again
varOrder(childVar, -varOrder(childVar)); //make positive again
//if some values in array are not -99
}

Expand All @@ -408,18 +412,20 @@ class SysModModel:public SysModule {
allNull = false;
}
if (allNull) {
print->printJson("remove allnulls", *childVar);
print->printJson("remove allnulls", childVar);
varChildren(var).remove(childVar);
}
}
else {
print->printJson("remove non valArray", *childVar);
print->printJson("remove non valArray", childVar);
varChildren(var).remove(childVar);
}

}
} //if new added
print->printJson("varPostDetails post", var);
USER_PRINTF("varPostDetails post ");
print->printVar(var);
USER_PRINTF("\n");

web->addResponse("details", "rowNr", rowNr);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Sys/SysModPins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void SysModPins::setup() {
ui->initText(tableVar, "pinDetails", nullptr, 256, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
for (forUnsigned8 rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++) {
USER_PRINTF("pinDetails[%d] d1:%s d2:%s\n", rowNr, getNthAllocatedPinObject(rowNr).details, pinObjects[0].details);
// USER_PRINTF("pinDetails[%d] d:%s\n", rowNr, getNthAllocatedPinObject(rowNr).details);
mdl->setValue(var, JsonString(getNthAllocatedPinObject(rowNr).details, JsonString::Copied), rowNr);
}
return true;
Expand Down Expand Up @@ -109,7 +109,7 @@ void SysModPins::setup() {
case f_ChangeFun: {
bool pinValue = var["value"];

USER_PRINTF("updateGPIO %s:=%d\n", mdl->varID(var), pinValue);
USER_PRINTF("chFun pin19 %s:=%d\n", mdl->varID(var), pinValue);

// softhack007: writing these pins on S3/C3/S2 may cause major problems (crashes included)
digitalWrite(19, pinValue?HIGH:LOW);
Expand Down Expand Up @@ -143,7 +143,7 @@ void SysModPins::allocatePin(unsigned8 pinNr, const char * owner, const char * d
}

void SysModPins::deallocatePin(unsigned8 pinNr, const char * owner) {
// USER_PRINTF("deallocatePin %d %s\n", pinNr, owner);
USER_PRINTF("deallocatePin %d %s\n", pinNr, owner);
if (pinNr < NUM_DIGITAL_PINS) {
if (strcmp(pinObjects[pinNr].owner, owner) != 0)
USER_PRINTF("deallocatePin %d: not owner %s!=%s", pinNr, owner, pinObjects[pinNr].owner);
Expand Down
26 changes: 19 additions & 7 deletions src/Sys/SysModPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void SysModPrint::loop() {
if (!setupsDone) setupsDone = true;
}

void SysModPrint::print(const char * format, ...) {
void SysModPrint::printf(const char * format, ...) {
va_list args;

va_start(args, format);
Expand Down Expand Up @@ -116,22 +116,34 @@ void SysModPrint::print(const char * format, ...) {
}

void SysModPrint::println(const __FlashStringHelper * x) {
print("%s\n", x);
printf("%s\n", x);
}

void SysModPrint::printVar(JsonObject var) {
char sep[3] = "";
char sep[3] = " ";
for (JsonPair pair: var) {
print("%s%s: %s", sep, pair.key(), pair.value().as<String>().c_str());
strcpy(sep, ", ");
if (pair.key() == "id") {
printf("%s%s", sep, pair.value().as<String>().c_str());
strcpy(sep, ", ");
}
else if (pair.key() == "value") {
printf(":%s", pair.value().as<String>().c_str());
}
else if (pair.key() == "n") {
printf("[");
for (JsonObject childVar: mdl->varChildren(var)) {
printVar(childVar);
}
printf("]");
}
}
}

void SysModPrint::printJson(const char * text, JsonVariantConst source) {
char resStr[1024];
serializeJson(source, resStr, sizeof(resStr));

print("%s %s\n", text, resStr);
printf("%s %s\n", text, resStr);
}

size_t SysModPrint::fFormat(char * buf, size_t size, const char * format, ...) {
Expand All @@ -148,5 +160,5 @@ size_t SysModPrint::fFormat(char * buf, size_t size, const char * format, ...) {
}

void SysModPrint::printJDocInfo(const char * text, JsonDocument source) {
print("%s (s:%u o:%u n:%u)\n", text, source.size(), source.overflowed(), source.nesting());
printf("%s (s:%u o:%u n:%u)\n", text, source.size(), source.overflowed(), source.nesting());
}
15 changes: 9 additions & 6 deletions src/Sys/SysModPrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#pragma once
#include "SysModule.h"

#define USER_PRINTF(x...) print->print(x)
#define USER_PRINT_FUNCTION(x...) //print->print(x)
#define USER_PRINT_NOT(x...) //print->print(x)
#define USER_PRINT_Async(x...) print->print(x)
#define USER_PRINTF(x...) print->printf(x)
#define prf(x...) print->printf(x)
// #define USER_PRINT_FUNCTION(x...) //print->printf(x)
// #define USER_PRINT_NOT(x...) //print->printf(x)
// #define USER_PRINT_Async(x...) print->printf(x)

class SysModPrint:public SysModule {

Expand All @@ -25,11 +26,13 @@ class SysModPrint:public SysModule {
void setup();
void loop();

//generic print function (based on printf)
void print(const char * format, ...);
//generic print function
void printf(const char * format, ...);

//not used yet
void println(const __FlashStringHelper * x);

//print var as id:value + [childVars recursively]
void printVar(JsonObject var);

void printJson(const char * text, JsonVariantConst source);
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty

//create new var
if (differentParents || var.isNull()) {
USER_PRINTF("initVar create new %s var: %s->%s\n", type, parentId?parentId:"", id); //parentId not null otherwise crash
USER_PRINTF("initVar new %s var: %s->%s\n", type, parentId?parentId:"", id); //parentId not null otherwise crash
if (parent.isNull()) {
JsonArray vars = mdl->model->as<JsonArray>();
var = vars.add<JsonObject>();
Expand All @@ -106,7 +106,7 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty
if (!var.isNull()) {
if (var["type"].isNull() || var["type"] != type) {
var["type"] = JsonString(type, JsonString::Copied);
print->printJson("initVar set type", var);
// print->printJson("initVar set type", var);
}

if (var["ro"].isNull() || mdl->varRO(var) != readOnly) mdl->varRO(var, readOnly);
Expand Down
22 changes: 11 additions & 11 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ class SysModUI: public SysModule {
size_t funNr = var["fun"];
if (funNr < varFunctions.size()) {
result = varFunctions[funNr](var, rowNr, funType);
// if (result) { //send rowNr = 0 if no rowNr
// //only print vars with a value
// if (!var["value"].isNull() && funType != f_ValueFun) {
// if (var["id"] == "fixFirst") {
// USER_PRINTF("%sFun %s", funType==f_ValueFun?"val":funType==f_UIFun?"ui":funType==f_ChangeFun?"ch":funType==f_AddRow?"add":funType==f_DelRow?"del":"other", mdl->varID(var));
// if (rowNr != UINT8_MAX)
// USER_PRINTF("[%d] = %s\n", rowNr, var["value"][rowNr].as<String>().c_str());
// else
// USER_PRINTF(" = %s\n", var["value"].as<String>().c_str());
// }
// }
if (result && !mdl->varRO(var)) { //send rowNr = 0 if no rowNr
//only print vars with a value and not valuefun as that changes a lot due to insTbl clTbl etc (tbd)
// if (!var["value"].isNull() &&
if (funType != f_ValueFun) {
prf("%sFun %s", funType==f_ValueFun?"val":funType==f_UIFun?"ui":funType==f_ChangeFun?"ch":funType==f_AddRow?"add":funType==f_DelRow?"del":"other", mdl->varID(var));
if (rowNr != UINT8_MAX)
prf("[%d] = %s\n", rowNr, var["value"][rowNr].as<String>().c_str());
else
prf(" = %s\n", var["value"].as<String>().c_str());
}
}
}
else
USER_PRINTF("dev callVarFun function nr %s outside bounds %d >= %d\n", mdl->varID(var), funNr, varFunctions.size());
Expand Down
Loading

0 comments on commit 956cd9f

Please sign in to comment.