Source/KSCrash/Recording/Monitors/KSCrashMonitor_AppState.c
/** Save the persistent state portion of a crash context.
 *
 * @param path The path to the file to create.
 *
 * @return true if the operation was successful.
 */
Save the persistent state portion of a crash context.
@param path The path to the file to create.

@return true if the operation was successful.
Save the persistent state portion of a crash context . @param path The path to the file to create . @return true if the operation was successful .
static bool saveState(const char* const path)
{
    int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
    if(fd < 0)
    {
        KSLOG_ERROR("Could not open file %s for writing: %s",
                    path,
                    strerror(errno));
        return false;
    }
    KSJSONEncodeContext JSONContext;
    ksjson_beginEncode(&JSONContext,
                       true,
                       addJSONData,
                       &fd);
    int result;
    if((result = ksjson_beginObject(&JSONContext, NULL)) != KSJSON_OK)
    {
        goto done;
    }
    if((result = ksjson_addIntegerElement(&JSONContext,
                                          kKeyFormatVersion,
                                          kFormatVersion)) != KSJSON_OK)
    {
        goto done;
    }
    if((result = ksjson_addBooleanElement(&JSONContext,
                                          kKeyCrashedLastLaunch,
                                          g_state.crashedThisLaunch)) != KSJSON_OK)
    {
        goto done;
    }
    if((result = ksjson_addFloatingPointElement(&JSONContext,
                                                kKeyActiveDurationSinceLastCrash,
                                                g_state.activeDurationSinceLastCrash)) != KSJSON_OK)
    {
        goto done;
    }
    if((result = ksjson_addFloatingPointElement(&JSONContext,
                                                kKeyBackgroundDurationSinceLastCrash,
                                                g_state.backgroundDurationSinceLastCrash)) != KSJSON_OK)
    {
        goto done;
    }
    if((result = ksjson_addIntegerElement(&JSONContext,
                                          kKeyLaunchesSinceLastCrash,
                                          g_state.launchesSinceLastCrash)) != KSJSON_OK)
    {
        goto done;
    }
    if((result = ksjson_addIntegerElement(&JSONContext,
                                          kKeySessionsSinceLastCrash,
                                          g_state.sessionsSinceLastCrash)) != KSJSON_OK)
    {
        goto done;
    }
    result = ksjson_endEncode(&JSONContext);
done:
    close(fd);
    if(result != KSJSON_OK)
    {
        KSLOG_ERROR("%s: %s",
                    path, ksjson_stringForError(result));
        return false;
    }
    return true;
}
static bool saveState ( const char * const path ) { int fd = open ( path , O_RDWR | O_CREAT | O_TRUNC , 0644 ) ; if ( fd < 0 ) { KSLOG_ERROR ( " " , path , strerror ( errno ) ) ; return false ; } KSJSONEncodeContext JSONContext ; ksjson_beginEncode ( & JSONContext , true , addJSONData , & fd ) ; int result ; if ( ( result = ksjson_beginObject ( & JSONContext , NULL ) ) != KSJSON_OK ) { goto done ; } if ( ( result = ksjson_addIntegerElement ( & JSONContext , kKeyFormatVersion , kFormatVersion ) ) != KSJSON_OK ) { goto done ; } if ( ( result = ksjson_addBooleanElement ( & JSONContext , kKeyCrashedLastLaunch , g_state . crashedThisLaunch ) ) != KSJSON_OK ) { goto done ; } if ( ( result = ksjson_addFloatingPointElement ( & JSONContext , kKeyActiveDurationSinceLastCrash , g_state . activeDurationSinceLastCrash ) ) != KSJSON_OK ) { goto done ; } if ( ( result = ksjson_addFloatingPointElement ( & JSONContext , kKeyBackgroundDurationSinceLastCrash , g_state . backgroundDurationSinceLastCrash ) ) != KSJSON_OK ) { goto done ; } if ( ( result = ksjson_addIntegerElement ( & JSONContext , kKeyLaunchesSinceLastCrash , g_state . launchesSinceLastCrash ) ) != KSJSON_OK ) { goto done ; } if ( ( result = ksjson_addIntegerElement ( & JSONContext , kKeySessionsSinceLastCrash , g_state . sessionsSinceLastCrash ) ) != KSJSON_OK ) { goto done ; } result = ksjson_endEncode ( & JSONContext ) ; done : close ( fd ) ; if ( result != KSJSON_OK ) { KSLOG_ERROR ( " " , path , ksjson_stringForError ( result ) ) ; return false ; } return true ; }
Save the persistent state portion of a crash context.
Save the persistent state portion of a crash context .
// Record this launch crashed state into "crashed last launch" field.