Ignore:
Timestamp:
Feb 25, 2019, 1:57:11 PM (5 years ago)
Author:
knut
Message:

Added some recent changes from trunk (r889), including some new utility functions and exception handling and new (conditional) definition of type bool. Added some new logic concerning Python and Mono environment and search paths. Fixed problem with Mono updateStatus function. Changed response_print.h to #include locale.h unconditionally and xlocale.h conditionally; xlocale.h is non-standard and can probably be dropped.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/prototype-v0/zoo-project/zoo-kernel/service.c

    r890 r896  
    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
     
    507511 */
    508512void addToMap(map* m,const char* n,const char* v){
    509   if(hasKey(m,n)==false){
    510     map* _cursor=m;
    511     while(_cursor->next!=NULL){
    512       _cursor=_cursor->next;
    513     }
    514     _cursor->next=createMap(n,v);
    515   }
    516   else{
    517     map *tmp=getMap(m,n);
    518     if(tmp->value!=NULL)
    519       free(tmp->value);
    520     tmp->value=zStrdup(v);
    521   }
     513    if (m != NULL) { // knut: add NULL-pointer check
     514        if (hasKey(m, n) == false) {
     515            map* _cursor = m;
     516            while (_cursor->next != NULL) {
     517                _cursor = _cursor->next;
     518            }
     519            _cursor->next = createMap(n, v);
     520        }
     521        else {
     522            map *tmp = getMap(m, n);
     523            if (tmp->value != NULL)
     524                free(tmp->value);
     525            tmp->value = zStrdup(v);
     526        }
     527    }
    522528}
    523529
     
    16901696  m=&trorf;
    16911697}
     1698
     1699/**
     1700 * Verify that a map has a value
     1701 *
     1702 * @param map pointer to map that should be checked
     1703 * @return true if map has a value or false if value is missing/empty/NULL
     1704 */
     1705bool nonempty(map* map) {
     1706        return (map != NULL && map->value != NULL && strlen(map->value) > 0 && strcmp(map->value, "NULL") != 0);
     1707}
     1708
     1709/**
     1710 * Verify that a particular map value exists in a maps
     1711 * data structure, and obtain that value
     1712 *
     1713 * @param source pointer to maps structure
     1714 * @param node name of maps node to search
     1715 * @param key name of map node to find
     1716 * @param kvp address to the map* if it exists, otherwise NULL
     1717 * @return true if map has a value or false if value is missing/NULL
     1718 *
     1719 * @note The map assigned to kvp is owned by the source maps
     1720 */
     1721bool hasvalue(maps* source, const char* node, const char* key, map** kvp) {
     1722        *kvp = getMapFromMaps(source, node, key);
     1723        return (*kvp != NULL && (*kvp)->value != NULL &&
     1724                strlen((*kvp)->value) > 0 && strcmp((*kvp)->value, "NULL") != 0);
     1725}
     1726
     1727/*
     1728 * Set error message in configuration maps
     1729 *
     1730 * @param conf reference to configuration maps
     1731 * @param service name of service
     1732 * @param exc WPSException code
     1733 * @param message exception text (default: exception text in WPS specification)
     1734 */
     1735void setErrorMessage(maps*& conf, const char* service, WPSException exc, const char* message) {
     1736
     1737        if (message == NULL) {
     1738                message = WPSExceptionText[exc];
     1739        }
     1740
     1741        size_t len = strlen(service) + strlen(": ") + strlen(message) + strlen(": ") + strlen(WPSExceptionCode[exc]) + 16;
     1742        char* msg = (char*)malloc(len * sizeof(char));
     1743
     1744        if (msg != NULL) {
     1745                snprintf(msg, len * sizeof(char), "\n%s: %s: %s\n", service, message, WPSExceptionCode[exc]);
     1746                setMapInMaps(conf, "lenv", "message", msg);
     1747                free(msg);
     1748        }
     1749}
     1750
     1751void logMessage(const char* source, const char* function, int line, const char* file, const char* message) { //, const char* source, const char* function, int line) {
     1752
     1753        size_t msglen = 512;
     1754        const char empty[] = "";
     1755
     1756        FILE* log;
     1757
     1758        // system time, process time [nanoseconds]   
     1759        unsigned long long sys_t, proc_t;
     1760
     1761        // processor time consumed by the program:
     1762        clock_t t = clock();
     1763
     1764        // system time:
     1765        std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
     1766
     1767        std::time_t now_t = std::chrono::system_clock::to_time_t(now);
     1768        std::tm* tm = localtime(&now_t);
     1769        char* str = asctime(tm);
     1770        str[strlen(str) - 1] = '\0'; // remove newline
     1771
     1772        sys_t = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
     1773        //proc_t = (unsigned long long)(1.0e9*t/CLOCKS_PER_SEC);
     1774        proc_t = t;
     1775
     1776        if (message != NULL) {
     1777                msglen += strlen(message);
     1778        }
     1779        else {
     1780                message = empty;
     1781        }
     1782        //getLastErrorMessage(); // cgiScriptName 
     1783        char* text = (char*)malloc(sizeof(char)*msglen);
     1784
     1785        snprintf(text, msglen, "pid: %d %s line %d %s() %s systime: %lld ns ticks: %lld %s\n",
     1786                _getpid(), source, line, function, str, sys_t, proc_t, message); // __FILE__ __LINE__ __func__ //
     1787
     1788        if (file != NULL && (log = fopen(file, "a+")) != NULL) {
     1789                fputs(text, log);
     1790                fclose(log);
     1791        }
     1792        else {
     1793#ifdef MSG_LOG_FILE
     1794                if ((log = fopen(MSG_LOG_FILE, "a+")) != NULL) {
     1795                        fputs(text, log);
     1796                        fclose(log);
     1797                }
     1798#endif
     1799        }
     1800
     1801        if (text != NULL) free(text);
     1802}
     1803
     1804// knut:
     1805// Example:
     1806// zooLog;
     1807// zooLogMsg(NULL, getLastErrorMessage());
     1808// zooLogMsg("log.txt", getLastErrorMessage());
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