B4R Question How substitute "log()" B4R IDE function ?

peacemaker

Expert
Licensed User
Longtime User
HI, All and @Erel

If i need to send all logs to a SD-card - how to replace all IDE log() calls with variable qty of arguments with my sub like MyLog(str1, str2, ..., strX) ?
 

peacemaker

Expert
Licensed User
Longtime User
Something like that, i guess:
B4X:
Sub SDlog(s() As String)
    Dim t As String
    For i = 0 To s.Length - 1
        t = JoinStrings(Array As String(t, s(i)))
    Next
    t = JoinStrings(Array As String(t, CRLF))
    Log(t)
    If esp_sd.Ready_flag Then
        esp_sd.Add_Sys_log(t.GetBytes)
    End If
End Sub

esp_sd module:
Public Sub Add_Sys_Log(line() As Byte)
    YEAR = esp_ds1307.Year
    MONTH = esp_ds1307.Month
    DATE = esp_ds1307.Date
    HOUR = esp_ds1307.Hour
    WriteString("/syslog.txt", line)
    DelayMicroseconds(10)
End Sub

Private Sub WriteString(key_string() As Byte, value() As Byte) As Boolean
    Dim source As KeyValueType
    source.key = key_string
    source.value = value
    Dim saved As Byte = RunNative("save_csv_line", source)
    If saved = 0 Then
        Ready_flag = False
        CallSubPlus("Start_SD", 5000, 1)
        Return False
    Else
        Ready_flag = True
    End If
    'Main.SDlog(Array As String("Saved to file")
    Return (saved = 1)
End Sub

#if C
#include "FS.h"
#include "SD.h"
#include "SPI.h"

B4R::Object returnvalue_esp_sp;

bool appendFile(fs::FS &fs, const char *path, const char *message) {
  //Serial.printf("Appending to file: %s\n", path);
    bool res;
  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    //Serial.println("Failed to open file for appending");
    return false;
  }
  res = file.print(message);
  if (res) {
    //Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.flush();
  file.close();
  return res;
}

B4R::Object* setup_sd(B4R::Object* o){
    bool begun = SD.begin();
    if (!begun) {
        //Serial.println("Card Mount Failed");
        return returnvalue_esp_sp.wrapNumber((byte)0);
    }else{
        return returnvalue_esp_sp.wrapNumber((byte)1);
    }
}

B4R::Object* save_csv_line(B4R::Object* passedvar){
    _keyvaluetype* tmp = (_keyvaluetype*)B4R::Object::toPointer(passedvar); //structure from B4R
 
    B4R::Array* b = tmp->key;
    char* c = (char*)b->data; //file name, do not forget slash "/file.txt"
    //String key = (String)c;
 
    B4R::Array* b2 = tmp->value;
    char* c2 = (char*)b2->data; //test line for saving to file
    //String value = (String)c2;

    bool res = appendFile(SD, c, c2);
    return returnvalue_esp_sp.wrapNumber((byte)res);
}

B4R::Object* freespace_sd(B4R::Object* o){
    float result;
    uint64_t total = SD.totalBytes() / (1024 * 1024);
    //Serial.printf("totalBytes: %llu MB\n", total);
    uint64_t used = SD.usedBytes() / (1024 * 1024);
    //Serial.printf("usedBytes: %llu MB\n", used);
    result = ((float)total - (float)used) / (float)total * 100;
    //Serial.print("Free %: ");
    //Serial.print(used);
   
       return returnvalue_esp_sp.wrapNumber((float)result);
}

B4R::Object* find_file(B4R::Object* o){
    B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
    char* fname = (char*)b->data;

    if (!SD.exists(fname))
        return returnvalue_esp_sp.wrapNumber((byte)0); //non-existent
    else
        return returnvalue_esp_sp.wrapNumber((byte)1);
}

B4R::Object* create_dir(B4R::Object* o){
    B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
    char* fname = (char*)b->data;

    if (!SD.mkdir(fname))
        return returnvalue_esp_sp.wrapNumber((byte)0); //failed
    else
        return returnvalue_esp_sp.wrapNumber((byte)1);
}
bool deleteFile(fs::FS &fs, const char *path) {
  //Serial.printf("Deleting file: %s\n", path);
  bool res = fs.remove(path);
  return res;
}

bool write_sys_log(fs::FS &fs, const char *path, const char *newline) {
  String data;
  File file = fs.open(path);
  if (file) {
    data = file.readString();
    file.close();
  }
 
  int limit = 10000; //max file size
  data = data + (String)newline;

  if (data.length() > limit) data = data.substring(data.length() - limit);
  deleteFile(SD, path);
  return appendFile(SD, path, data.c_str());
}

B4R::Object* looped_log(B4R::Object* o){
    B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
    char* addline = (char*)b->data;

    if (!write_sys_log(SD, "/syslog.txt", addline))
        return returnvalue_esp_sp.wrapNumber((byte)0);
    else
        return returnvalue_esp_sp.wrapNumber((byte)1);
}
#End If
 
Upvote 0
Top