Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

There has to be a better way to do this...

$
0
0
I have this block of code that works perfectly and does exactly what I need it to do, but I feel like it's a dirty, awful hack of a solution and there must be a better way to get it to work. What I'm doing is reading in a SAS log file and writing the contents into a rich text box. I'm preserving the color coding that one normally sees in SAS output by checking each line as it's read to see if it meets certain conditions, then changing the text color to match the coding scheme used in SAS; because the messages written in the log can wrap beyond a single line, I'm also checking to see if the next line after a color coded line is indented to indicate that it's still a part of the previous line (which is how the output from SAS is formatted) and needs to be colored as well.

I'm also collecting the first line of any warnings or errors found in the output into an array in order to display an error/warning summary to the user once the log is done being printed to the textbox. The resulting code looks like this:

        oInFile = oInFileO.OpenTextFile(chrDrive & ":\SASH2\Log.log", IOMode.ForReading)

        strSText = ""
        Do While Not oInFile.AtEndOFStream
            If strSText Like "ERROR:*" Then
                aryErrors.Add(strSText)
                With frmSAS.txtLog
                    .Selectionstart = .TextLength
                    .SelectionColor = Color.Red
                    .AppendText(strSText & vbNewLine)
                    .SelectionColor = .ForeColor
                End With
                strSText = oInFile.Readline
                If Len(strSText) > 5 Then
                    Do While strSText Like "    *" And Not oInFile.AtEndOFStream
                        With frmSAS.txtLog
                            .Selectionstart = .TextLength
                            .SelectionColor = Color.Red
                            .AppendText(strSText & vbNewLine)
                            .SelectionColor = .ForeColor
                        End With
                        strSText = oInFile.Readline
                    Loop
                End If
            ElseIf strSText Like "NOTE:*" Then
                With frmSAS.txtLog
                    .Selectionstart = .TextLength
                    .SelectionColor = Color.Blue
                    .AppendText(strSText & vbNewLine)
                    .SelectionColor = .ForeColor
                End With
                strSText = oInFile.Readline
                If Len(strSText) > 5 Then
                    Do While strSText Like "    *" And Not oInFile.AtEndOFStream
                        With frmSAS.txtLog
                            .Selectionstart = .TextLength
                            .SelectionColor = Color.Blue
                            .AppendText(strSText & vbNewLine)
                            .SelectionColor = .ForeColor
                        End With
                        strSText = oInFile.Readline
                    Loop
                End If
            ElseIf strSText Like "WARNING:*" Then
                aryErrors.Add(strSText)
                With frmSAS.txtLog
                    .Selectionstart = .TextLength
                    .SelectionColor = Color.Green
                    .AppendText(strSText & vbNewLine)
                    .SelectionColor = .ForeColor
                End With
                strSText = oInFile.Readline
                If Len(strSText) > 5 Then
                    Do While strSText Like "    *" And Not oInFile.AtEndOFStream
                        With frmSAS.txtLog
                            .Selectionstart = .TextLength
                            .SelectionColor = Color.Green
                            .AppendText(strSText & vbNewLine)
                            .SelectionColor = .ForeColor
                        End With
                        strSText = oInFile.Readline
                    Loop
                End If
            Else
                With frmSAS.txtLog
                    .Selectionstart = .TextLength
                    .SelectionColor = Color.Black
                    .AppendText(strSText & vbNewLine)
                    .SelectionColor = .ForeColor
                End With
                strSText = oInFile.Readline
            End If
        Loop
        oInFile.Close()



Surely there's a better solution than this mess! I've attached a SAS log file like one I'd be reading in if anyone wants to give it a try (please note there are an enormous number of errors and warnings in the log file; I did that on purpose so I'd have a diverse log file to work with). I'd appreciate any insight you guys might have.


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>