Ignore:
Timestamp:
Jan 3, 2019, 12:44:57 PM (5 years ago)
Author:
knut
Message:

Added some new logging functionality (function logMessage(), macros zooLog, zooLogMsg). Added utility functions setErrorMessage(), hasvalue(), and nonempty() in service.c. Added enum WPSException and arrays WPSExceptionText and WPSExceptionCode (see also function setErrorMessage). New conditional definition of type bool in service.c (to fix issue with bool). Null pointer check in function addToMap. Added missing return values and explicit type casts in some functions. Removed Windows-specific code in function dumpBackFinalFile (zoo_service_loader.c) that may cause error for async raw data output in formats other than XML.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/service.c

    r868 r889  
    2525#include "service.h"
    2626
     27// knut: time utilities required for new log function (logMessage)
     28#include <ctime>
     29#include <chrono>
     30#include <process.h>
    2731
    2832#if defined(_MSC_VER) && _MSC_VER < 1800
     
    450454 */
    451455void addToMap(map* m,const char* n,const char* v){
    452   if(hasKey(m,n)==false){
    453     map* _cursor=m;
    454     while(_cursor->next!=NULL){
    455       _cursor=_cursor->next;
    456     }
    457     _cursor->next=createMap(n,v);
    458   }
    459   else{
    460     map *tmp=getMap(m,n);
    461     if(tmp->value!=NULL)
    462       free(tmp->value);
    463     tmp->value=zStrdup(v);
     456  if (m != NULL) { // knut: add NULL-pointer check
     457    if(hasKey(m,n)==false){
     458      map* _cursor=m;
     459      while(_cursor->next!=NULL){
     460        _cursor=_cursor->next;
     461      }
     462      _cursor->next=createMap(n,v);
     463    }
     464    else{
     465      map *tmp=getMap(m,n);
     466      if(tmp->value!=NULL)
     467        free(tmp->value);
     468      tmp->value=zStrdup(v);
     469    }
    464470  }
    465471}
     
    15971603}
    15981604
     1605/**
     1606 * Verify that a map has a value
     1607 *
     1608 * @param map pointer to map that should be checked
     1609 * @return true if map has a value or false if value is missing/empty/NULL
     1610 */
     1611bool nonempty( map* map ) {
     1612    return ( map != NULL && map->value != NULL && strlen(map->value) > 0 && strcmp(map->value, "NULL") != 0 );
     1613}
     1614
     1615/**
     1616 * Verify that a particular map value exists in a maps
     1617 * data structure, and obtain that value
     1618 *
     1619 * @param source pointer to maps structure
     1620 * @param node name of maps node to search
     1621 * @param key name of map node to find
     1622 * @param address to the map* if it exists, otherwise NULL
     1623 * @return true if map has a value or false if value is missing/NULL
     1624 */
     1625bool hasvalue( maps* source, const char* node, const char* key, map** kvp ) {
     1626    *kvp = getMapFromMaps(source, node, key);
     1627    return ( *kvp != NULL && (*kvp)->value != NULL &&
     1628             strlen((*kvp)->value) > 0 && strcmp((*kvp)->value, "NULL") != 0 );
     1629}
     1630
     1631/*
     1632 * Set error message in configuration maps
     1633 *
     1634 * @param conf reference to configuration maps
     1635 * @param service name of service
     1636 * @param exc WPSException code
     1637 * @param message exception text (default: exception text in WPS specification)
     1638 */
     1639void setErrorMessage( maps*& conf, const char* service, WPSException exc, const char* message ) {
     1640 
     1641  if (message == NULL) {
     1642    message = WPSExceptionText[exc];
     1643  }
     1644
     1645        size_t len = strlen( service ) + strlen(": ") + strlen( message ) + strlen(": ") + strlen(WPSExceptionCode[exc]) + 16;
     1646        char* msg = (char*) malloc( len * sizeof(char) );
     1647
     1648        if (msg != NULL) {
     1649                snprintf( msg, len*sizeof(char), "\n%s: %s: %s\n", service, message, WPSExceptionCode[exc] );
     1650                setMapInMaps( conf, "lenv", "message", msg );
     1651                free( msg );
     1652        }       
     1653}
     1654
     1655void logMessage(const char* source, const char* function, int line, const char* file, const char* message) { //, const char* source, const char* function, int line) {
     1656 
     1657  size_t msglen = 512;
     1658  const char empty[] = "";
     1659 
     1660  FILE* log;
     1661 
     1662  // system time, process time [nanoseconds]   
     1663  unsigned long long sys_t, proc_t;
     1664 
     1665  // processor time consumed by the program:
     1666  clock_t t = clock();
     1667   
     1668  // system time:
     1669  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
     1670 
     1671  std::time_t now_t = std::chrono::system_clock::to_time_t( now );
     1672  std::tm* tm = localtime( &now_t );
     1673        char* str = asctime(tm);
     1674  str[strlen(str)-1] = '\0'; // remove newline
     1675 
     1676  sys_t = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
     1677  //proc_t = (unsigned long long)(1.0e9*t/CLOCKS_PER_SEC);
     1678  proc_t = t;
     1679 
     1680  if ( message != NULL ) {
     1681     msglen += strlen(message);
     1682  } 
     1683  else {
     1684    message = empty;
     1685  }
     1686  //getLastErrorMessage(); // cgiScriptName 
     1687  char* text = (char*) malloc( sizeof(char)*msglen );
     1688 
     1689  snprintf( text, msglen, "pid: %d %s line %d %s() %s systime: %lld ns ticks: %lld %s\n",
     1690    _getpid(), source, line, function, str, sys_t, proc_t, message ); // __FILE__ __LINE__ __func__ //
     1691 
     1692  if ( file != NULL && (log = fopen( file, "a+" )) != NULL ) {
     1693    fputs( text, log );
     1694    fclose( log );
     1695  }
     1696  else {
     1697    #ifdef MSG_LOG_FILE
     1698    if ( (log = fopen( MSG_LOG_FILE, "a+" )) != NULL ) {
     1699      fputs( text, log );
     1700      fclose( log );
     1701    }     
     1702    #endif
     1703  }
     1704 
     1705  if ( text != NULL ) free( text );
     1706}
     1707
     1708// knut:
     1709// Example:
     1710// zooLog;
     1711// zooLogMsg(NULL, getLastErrorMessage());
     1712// zooLogMsg(log.txt, getLastErrorMessage());
     1713
    15991714#ifdef WIN32
    16001715#ifndef USE_MS
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png