Re: [mh] Your help re Genericising Error and log handling

Giles Godart-Brown <[email protected]>
Newsgroups gmane.comp.misc.misterhouse.user
Message-ID <[email protected]>
Summarising the comments ; the desire is for print_log to be called 
unchanged for legacy apps, but to allow developers to intercept 
print_log requests and route them accordingly (email, SMS, growl, print, 
tasker, IFTTT...)  and to do this with the minimum impact

Here is what I have built and tested today. If its OK with the forum, 
I'll create a pull request.

<changes to mh>

# 13-jan-2021 new print_log added and old print_log renamed 
print_log_simple to allow
# 1) optional additional parameters severity and source
#    severity should be one of EMERGENCY, ALERT, CRITICAL,
#    ERROR, WARNING, NOTICE, INFORMATIONAL, or DEBUG
#    message is reformatted as [source] severity message
# 2) substitution of print_log_private() if it exists and severity is 
defined
#    otherwise functions as the old print_log
sub print_log {
     my ($message,$severity,$source) = @_;

     if (defined($severity)) {
         if (defined(&print_log_private)) {
             print_log_private($message,$severity,$source);
             return;
         }
         $message = $severity . " " . $message;
     }
     if (defined($source)) {
         $message = "[" . $source . "] ".  $message;
     }
     print_log_simple($message);
}


# renamed from print_log
sub print_log_simple {

</changes to mh>

*And here are the test cases;*

*test 1 without  a print_log_private()*

print_log("message");    produces  13/01/2021 09:23:48 message

print_log("message","WARNING"); produces  13/01/2021 09:23:48 WARNING 
message

print_log("message","ERROR","source"); produces 13/01/2021 09:23:48 
[source] ERROR message

t*est 2 with a print_log_private() *that prepends 'print_log_private' to 
the source and calls print_log_simple() (see attached)

print_log("message"); produces the following because severity wasn't set 
and so print_log_private not called 13/01/2021 09:27:23 message

print_log("message","WARNING"); produces 13/01/2021 09:27:23 
[print_log_private] WARNING message

print_log("message","ERROR","source"); produces 13/01/2021 09:27:23 
[print_log_private-source] ERROR message

Giles

On 12/01/2021 20:10, Brian M wrote:
>
>
> On 1/12/21 11:00 AM, Rick Steeves wrote:
>> Might it not make more sense to modify print_log, so that, as is, it 
>> keeps working as expected, but in any case that print_log was called 
>> with more than one parameter then new things happened?
>>
>> That makes all the existing code keep working, but new code (or 
>> people modifying existing code) could just add parameters.
>>
>> If you were going to do a search/replace for print_log, you're still 
>> going to have to find all the cases of it and manually adjust them 
>> for what you want to have happen.
>>
>> That would avoid creating a new function, have it keep working as-is 
>> in all the existing code, and let it be adjusted on a case-by-case 
>> basis. 
>
>
> I agree with Rick.
>
>
>
>
> ________________________________________________________
> To unsubscribe from this list, go to: 
> https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>

________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
print_log_private.pl (text/x-perl-script, 1.6 KB)
sub print_log_private {
	my ($message,$severity,$source) = @_;
	# create a message to print
	# WARNING DO NOT CALL print_log FROM HERE, it will LOOP, use print_log_simple
	my $local_message = $message;
	
	if (defined($severity)) {
		$local_message = $severity . " " . $local_message;
	}
	if (defined($source)) {
		$local_message = "[print_log_private-" . $source . "] ".  $local_message;
	} else {
		$local_message = "[print_log_private] ".  $local_message;
	}
	
	if ($severity eq "EMERGENCY" ) {
		# code to manage emergency messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "ALERT" ) {
		# code to manage alert messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "CRITICAL" ) {
		# code to manage critical messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "ERROR" ) {
		# code to manage error messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "WARNING" ) {
		# code to manage warning messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "NOTICE" ) {
		# code to manage notice messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "INFORMATIONAL" ) {
		# code to manage info messages goes here
		print_log_simple($local_message);
		return;
	} elsif ($severity eq "DEBUG" ) {
		# code to manage debug messages goes here
		print_log_simple($local_message);
		return;
	}
    print_log_simple("[print_log_private] WARNING invalid severity $severity");
    print_log_simple($local_message);
    return;   
	
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.