Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

write2log xbasic function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    write2log xbasic function

    Hey all, I've written a basic sql error logging/debugging xbasic function. Basically, it lets me call write2log("Error","This is the error") from anywhere in xbasic. It will then insert a record in a logging table in my database.
    It includes the following information:
    1) The Ulink of the user that is logged in.
    2) The pagename or scriptname that the call occured on or in.
    3)The User agent of the browser the user was using
    4) The IP Address of the user (check local laws if you are allowed to capture this info)

    Code:
    'Date Created: 27-Jun-2011 01:34:31 PM
    'Last Updated: 23-Jul-2012 01:07:14 PM
    'Created By  : Sparticuz
    'Updated By  : Sparticuz
    FUNCTION write2log as v ( event as c, error as c = "" )
    
    
        dim cn as SQL::Connection
        dim flag as l 
        flag = cn.open("")
        if flag == .f. then 
            goto static_error
        end if 
    
    
        dim args as sql::arguments
        
        if variable_exists("Session") then
            if variable_exists("Session.__protected__ulink") then
                args.add("ulink",Session.__protected__ulink)
            else
                args.add("ulink","Not Logged In")
            end if
        else
            args.add("ulink","localhost")
        end if
        
        args.add("event",event)
        args.add("desc",error)
        
        if variable_exists("Request") then
            if variable_exists("Request.Variables.__pageName") then
                args.add("cur_page",Request.Variables.__pageName)
            else
                args.add("cur_page",Request.Script_name)
            end if
            args.add("user_agent",Request.UserAgent)
            args.add("ip",Request.Remote_Addr)
        else
            args.add("cur_page","localhost")
            args.add("user_agent","localhost")
            args.add("ip","127.0.0.1")
        end if
        
        dim sql as c
        sql = "INSERT INTO gen_log (`ulink`, `event`, `desc`, `cur_page`, `user_agent`, `ip`) VALUES (:ulink,:event,:desc,:cur_page,:user_agent,INET_ATON(:ip));"    
        flag = cn.Execute(sql,args)
        if flag == .f. then
            goto static_error
        end if
        
        cn.FreeResult()
        cn.Close()
        delete flag
        delete cn
        delete sql
        delete args
        end 
        
        static_error:
            'The log entry in the DB has failed.
            'Lets try a static text file instead
            'TODO
        end
        
    END FUNCTION
    Then the code for the table (mysql):
    Code:
    CREATE TABLE `gen_log` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`ulink` CHAR(36) NULL DEFAULT NULL,`event` VARCHAR(50) NULL DEFAULT NULL,`desc` TEXT NULL,`cur_page` VARCHAR(50) NULL DEFAULT NULL,`user_agent` VARCHAR(255) NULL DEFAULT NULL,`ip` INT(10) UNSIGNED NULL DEFAULT NULL COMMENT 'Use INET_ATON("ip") to convert to int and use INET_NTOA(num) to convert to ip.',`modified_date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`))COMMENT='Table used for logging purposes'COLLATE='latin1_swedish_ci'ENGINE=InnoDB;

    #2
    Re: write2log xbasic function

    Not to take anything away from Sparticuz, but here is one I wrote recently:

    Code:
    'Date Created: 05-Jul-2012 11:00:02 AM
    'Last Updated: 05-Jul-2012 01:35:27 PM
    'Created By  : pgreulich
    'Updated By  : pgreulich
    
    FUNCTION f_ErrorGet AS C (locVars AS P, vObject="" as C )
    ''use in script as f_ErrorGet(local_variables()) 
    ''or, e.g. as f_ErrorGet(local_variables(), "PUB_Users_A dialog") 
    
    Dim errDateTime as T
    Dim errScript as C
    Dim errCode as N
    Dim errLine as N
    Dim errMsg as C
    
    errDateTime = now()
    errObject = vObject
    errScript = error_script_get()
    errCode = error_code_get()
    errLine = error_line_number_get()
    errMsg = error_text_get()
    ''---------------------------------------------
    Dim SQL as c
    Dim conn as SQL::connection
    Dim args as SQL::Arguments
    
    args.add("errDateTime",errDateTime)
    args.add("errObject",vObject)
    args.add("errScript",errScript)
    args.add("errCode",errCode)
    args.add("errLine",errLine)
    args.add("errMsg",errMsg)
    
    vArgs = "(:errDateTime, :errObject, :errScript, :errCode, :errLine, :errMsg)"
    vFields = "(errDateTime, errObject, errScript, errCode, errLine, errMsg)"
    
    SQL = "INSERT INTO Error_Log " + vFields + " VALUES " + vArgs
    
    conn.open("::Name::System")							
    flag = conn.execute(SQL, args)
    conn.close()
    
    f_ErrorGet = errMsg
    'f_ErrorGet = conn.callResult.text
    
    END FUNCTION
    It is used in a script like this:

    Code:
    ON ERROR GOTO ERROR_HANDLER
    
    'xbasic code...
    
    flag = conn.execute(SQL, args)
    
    If flag = .f.
    	vMessage = "Unable to create new account."
    	vErrorText = conn.callResult.text
    	error_generate(vMessage + " SQL: " + vErrorText)
    End if
    
    'xbasic code...
    
    END
    ''=============================================================
    ERROR_HANDLER:
    f_ErrorGet(local_variables(),"PUB_ConfirmAccount.a5w") 
    
    vHelpMsg = f_HelpGet("PUB_ConfirmAccount.a5w_ERROR")
    
    errLine = ltrim(str(error_line_number_get()))
    session.message = "<br><br>System Error " + errLine + "<br>" + vMessage + "<br>" + vHelpMsg
    response.redirect("PUB_Message.a5w")
    End
    Or other variations thereof.

    Your mileage may vary!

    Note: needless to say, this is for the web.
    Peter
    AlphaBase Solutions, LLC

    [email protected]
    https://www.alphabasesolutions.com


    Comment


      #3
      Re: write2log xbasic function

      This is great. Mine is more for logging, whereas yours is more for error catching and alerting. I was eventually also going to add in line numbers and more for error catching/alerting too. (Maybe email notification for errors as well). I also use mine for user tracking. i.e. write2log("Login", :ulink)

      Comment


        #4
        Re: write2log xbasic function

        Yeah, the more the merrier! I use Alpha's built in security which has an option for a UDF that fires on login/logout and captures IP address etc and I send that to a login table. That works real well. I also have a log table and a function to capture certain script events and milestones - some of which will fire after hours. That way the system manager know if something failed that shouldn't have/or if it succeeded.
        Peter
        AlphaBase Solutions, LLC

        [email protected]
        https://www.alphabasesolutions.com


        Comment


          #5
          Re: write2log xbasic function

          I didn't even know there was a UDF for login/logout. I set up my security in v10 and have really, never looked at it again. Does it fire on all logouts? Or just when logout is called? (i.e. Does it fire on-user closes browser, session times out on server and never 'logs' out)

          Comment


            #6
            Re: write2log xbasic function

            No, only real logouts.

            See image...

            Security UDF.png
            Peter
            AlphaBase Solutions, LLC

            [email protected]
            https://www.alphabasesolutions.com


            Comment


              #7
              Re: write2log xbasic function

              Thanks I was needing just this.

              Comment

              Working...
              X