Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
sdk:log-to-syslog [2016/09/30 13:47]
juraschek [The Script]
sdk:log-to-syslog [2016/10/05 15:36]
juraschek [The Script] nicer line break without value break
Line 62: Line 62:
   ​   ​
 <code c status-to-syslog.are>​ <code c status-to-syslog.are>​
-/* DESC: A Script that can be used for a longtime logging of a NetModule Wireless Router.  +    ​/* DESC: A Script that can be used for a longtime logging of a NetModule Wireless Router.  
- * The upper part of the script is the logger template. The lower part is the real programm, where you describe the settings and workflow. +     ​* The upper part of the script is the logger template. The lower part is the real programm, where you describe the settings and workflow. 
- * Copyright (C) 2016 NetModule AG, Switzerland +     ​* Copyright (C) 2016 NetModule AG, Switzerland 
- */ +     ​*/ 
-     +     ​ 
-  +     ​ 
-template logger +    template logger 
-+    
-  +     ​ 
-    # give the constructor the array of fields to log +        # give the constructor the array of fields to log 
-    void logger(array logfields) { +        void logger(array logfields) { 
-        this.fields = logfields+            this.fields = logfields;
-    } +
-  +
-    int logNow(){ ​    +
-        //get current status output +
-        sections = mkarray("​system",​ "​wan",​ "​wwan",​ "​wlan",​ "​gnss",​ "​lan",​ "​openvpn",​ "​ipsec",​ "​dio",​ "​license"​);​ +
-        status = mkstruct();​ +
-        for(i = 0; i < length(sections);​ i++) { +
-            status=struct_merge(status,​ nb_status(sections[i]));+
         }         }
 +     
 +        int logNow(){ ​   ​
 +            MAX_LINE_LEN=180; ​  
 +            //get current status output
 +            sections = mkarray("​system",​ "​wan",​ "​wwan",​ "​wlan",​ "​gnss",​ "​lan",​ "​openvpn",​ "​ipsec",​ "​dio",​ "​license"​);​
 +            status = mkstruct();
 +            for(i = 0; i < length(sections);​ i++) {
 +                status=struct_merge(status,​ nb_status(sections[i]));​
 +            }
 +     
 +            line = "";​
 +        ​
 +            for(i = 0; i < length(this.fields);​ i++) {
 +                if(strlen(this.fields[i]) > 0 ) {
 +                    value = struct_get(status,​ this.fields[i]);​
 +                    if(strlen(value) == 0) {   
 +                        value = sprintf("​%s unknown",​ this.fields[i]);​
 +                    }
 +
 +                    if(strlen(this.fields[i+1]) > 0 ) {
 +                    nextvalue = struct_get(status,​ this.fields[i+1]);​
 +                    if(strlen(nextvalue) == 0) {   
 +                        nextvalue = sprintf("​%s unknown",​ this.fields[i+1]);​
 +                    }
 +                    }
    
-        ​line = "";​ +                    //line = sprintf('%s%s;', line,value);   ​ 
-        for(i = 0i < length(this.fields); i++) { +                    ​line ​sprintf("​%s%s:​%s##​ ", line, this.fields[i], value); 
-            ​if(strlen(this.fields[i]) > 0 ) { +                    if (strlen(line)+strlen(sprintf("​%s:%s##",​this.fields[i+i],​nextvalue))>​MAX_LINE_LEN) { 
-                value struct_get(status, this.fields[i]);​ +                        nb_syslog(line); 
-                if(strlen(value== 0) {    +                        line="";​ 
-                    value = sprintf("​%s ​unknown", this.fields[i]);​+                        } 
 +                        ​
                 }                 }
-                //line = sprintf('​%s%s;',​ line,​value); ​   
-                line = sprintf("​%s%s:​%s##​ ", line, this.fields[i],​ value); 
             }             }
 +            if (strlen(line) > 0) nb_syslog(line);​
 +     
 +            return 0;
         }         }
-        MAX_LINE_LEN=180+      
-        ​for(i=0; i<strlen(line);i=i+MAX_LINE_LEN) { +      
-                ​subline=substr(line,i,​MAX_LINE_LEN); +    } // of template logger 
-                ​nb_syslog("​%s",​ subline); +      
-        }+    if (argc < 3){ 
 +        printf("​usage:​ status_to_syslog.are [single|<​1-n seconds>​] <list of parameter>"​)
 +        ​exit(1); 
 +    } 
 +      
 +    if (argv[1] == "​single"​) 
 +        mode = 0; 
 +    else 
 +        mode = (intargv[1]; 
 +      
 +    elements = mkarray();​ 
 +    for (i = 2; <= argc; i++) { 
 +        ​elements ​array_merge(elementsargv[i]); 
 +    } 
 +    l = new logger(elements); 
 +      
 +    do { 
 +        ​l.logNow();​ 
 +        sleep(mode);​ 
 +    ​while(mode);​ 
 +    exit(0); 
  
-        return 0; 
-    } 
-  
-  
-} // of template logger 
-  
-if (argc < 3){ 
-    printf("​usage:​ status_to_syslog.are [single|<​1-n seconds>​] <list of parameter>"​);​ 
-    exit(1); 
-} 
-  
-if (argv[1] == "​single"​) 
-    mode = 0; 
-else 
-    mode = (int) argv[1]; 
-  
-elements = mkarray(); 
-for (i = 2; i <= argc; i++) { 
-    elements = array_merge(elements,​ argv[i]); 
-} 
-l = new logger(elements);​ 
  
-do { 
-    l.logNow(); 
-    sleep(mode);​ 
-} while(mode);​ 
-exit(0); 
  
 </​code>​ </​code>​