Previous topic

Heterogeneous Relation Graphs Example

Next topic

Generic Object System Example

Error handling and Debugging ExampleΒΆ

This code example shows the usage of the Error handling and Debugging module. It can also be found at speect/engine/examples/base/errdbg/errdbg_example.c, and be compiled with the WANT_EXAMPLES build option.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include "speect.h"


/************************************************************************************/
/*                                                                                  */
/* Function implementations                                                         */
/*                                                                                  */
/************************************************************************************/

void deep_nested(int i, s_erc *error)
{
    /*
     * a deeply nested function may not
     * have enough context to set an appropriate
     * error message, so it can just set an error.
     */
    if (i != 151623)
    {
        S_NEW_ERR(error, S_FAILURE);
        return;
    }

    S_CLR_ERR(error);
}


int *make_numbers(int d, s_erc *error)
{
    int *n;

    n = S_MALLOC(n, 1);
    if (n == NULL)
    {
        /*
         * fatal errors abort if the build switch
         * -DERROR_ABORT_FATAL=1 is used during cmake configuration,
         * see the README
         */
        S_FTL_ERR(error, S_MEMERROR,
                 "make_numbers",
                 "Failed to allocate memory");
        return NULL;
    }

    /* set a debug message with a debug level */
    S_DEBUG(S_DBG_INFO, "d is %d", d);

    if (d == 0)
        *n = 12;
    else
        *n = 151623;

    /*
     * call 'deep_nested' with
     * the error variable
     */
    deep_nested(*n, error);

    /*
     * check the error status received from
     * function 'deep_nested', and set an
     * error with some context information
     * if 'deep_nested' did not return S_SUCCESS
     *
     * In this example we use S_CONTERR, which will
     * just pass on the error received from 'deep_nested'
     */
    if (S_CHK_ERR(error, S_CONTERR,
             "make_numbers",
             "Number is not valid"))
        return n;

    /*
     * no error occured, clear the error
     * just to be safe
     */
    S_CLR_ERR(error);

    return n;
}


int main(void)
{
    s_erc error = S_SUCCESS; /* start of with a clean slate */
    int i;
    int *tmp;


    /* initialize speect */
    error = speect_init(NULL);
    if (error != S_SUCCESS)
    {
        printf("Failed to initialize Speect\n");
        return 1;
    }

    for (i = 0; i < 3; i++)
    {
        if (i == 0)
        {
            /* clear the error variable */
            S_CLR_ERR(&error);

            /* change the debugging level */
            s_set_errdbg_level(S_DBG_INFO, &error);

            /* check error and continue it */
            S_CHK_ERR(&error, S_CONTERR,
                     "main",
                     "Failed to set debug level, trying to continue");

            tmp = make_numbers(i, &error);

            /* check error from make_numbers and set a new error and context */
            if (S_CHK_ERR(&error, S_FAILURE,
                     "main",
                     "Number is not valid"))
            {
                /* debug with S_DBG_INFO level */
                S_DEBUG(S_DBG_INFO, "Found error with i = %d", 1);

                if (tmp != NULL)
                    S_FREE(tmp);
            }

            if (tmp != NULL)
                S_FREE(tmp);

            continue;
        }

        if (i == 1)
            /* set an new error with a context */
            S_CTX_ERR(&error, S_FAILURE,
                     "main",
                     "i = %d, is invalid", i);
        else
        {
            /* clear the error variable */
            S_CLR_ERR(&error);

            tmp = make_numbers(i, &error);

            /* check error from make_numbers and set a new error and context */
            if (S_CHK_ERR(&error, S_FAILURE,
                     "main",
                     "Number is not valid"))
            {
                /*
                 * debug with S_DBG_TRACE level,
                 * this debug will not do anything as it's level
                 * is higher than the debug level set at i = 0
                 */
                S_DEBUG(S_DBG_TRACE, "Found error with i = %d", 1);

                if (tmp != NULL)
                    S_FREE(tmp);
            }

            if (tmp != NULL)
                S_FREE(tmp);
        }
    }


    /* quit speect */
    error = speect_quit();
    if (error != S_SUCCESS)
    {
        printf("Call to 'speect_quit' failed\n");
        return 1;
    }

    return 0;
}