Skip to content

Commit b96fa6e

Browse files
committed
Refactor: Extract debug utilities to meet file size limits
- Created fortplot_debug_utils.f90 module for reusable debug infrastructure - Moved debug utility functions from fortplot_zlib_core.f90 - Updated zlib_compress_into to use new debug module - Reduced fortplot_zlib_core.f90 from 1054 to 986 lines (under 1000 line hard limit) - File size compliance now passes: 0 critical violations Fixes CI failure where verify-size-compliance step was blocking PR merge. Debug functionality preserved with FORTPLOT_ZLIB_DEBUG environment variable.
1 parent 946f79d commit b96fa6e

File tree

2 files changed

+86
-73
lines changed

2 files changed

+86
-73
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module fortplot_debug_utils
2+
!! Debug utilities for optional instrumentation
3+
implicit none
4+
5+
private
6+
public :: is_debug_enabled, log_debug_message
7+
8+
logical, save :: debug_initialized = .false.
9+
logical, save :: debug_enabled_state = .false.
10+
11+
contains
12+
13+
pure function to_lower_char(ch) result(lower)
14+
!! Lower-case conversion for ASCII characters
15+
character(len=1), intent(in) :: ch
16+
character(len=1) :: lower
17+
integer :: code
18+
19+
lower = ch
20+
code = iachar(ch)
21+
if (code >= iachar('A') .and. code <= iachar('Z')) then
22+
lower = achar(code + 32)
23+
end if
24+
end function to_lower_char
25+
26+
logical function parse_bool_env(value)
27+
!! Interpret an environment variable as boolean
28+
character(len=*), intent(in) :: value
29+
character(len=:), allocatable :: trimmed
30+
integer :: i
31+
32+
trimmed = trim(adjustl(value))
33+
34+
if (len(trimmed) == 0) then
35+
parse_bool_env = .true.
36+
return
37+
end if
38+
39+
do i = 1, len(trimmed)
40+
trimmed(i:i) = to_lower_char(trimmed(i:i))
41+
end do
42+
43+
select case (trimmed)
44+
case ('0', 'false', 'off', 'no')
45+
parse_bool_env = .false.
46+
case default
47+
parse_bool_env = .true.
48+
end select
49+
end function parse_bool_env
50+
51+
subroutine ensure_debug_init(env_var)
52+
!! Lazy initialization for debug flag from environment variable
53+
character(len=*), intent(in) :: env_var
54+
character(len=32) :: env_value
55+
integer :: status
56+
57+
if (debug_initialized) return
58+
59+
call get_environment_variable(env_var, env_value, status=status)
60+
if (status == 0) then
61+
debug_enabled_state = parse_bool_env(env_value)
62+
else
63+
debug_enabled_state = .false.
64+
end if
65+
debug_initialized = .true.
66+
end subroutine ensure_debug_init
67+
68+
logical function is_debug_enabled(env_var)
69+
!! Query if debug is enabled for given environment variable
70+
character(len=*), intent(in) :: env_var
71+
call ensure_debug_init(env_var)
72+
is_debug_enabled = debug_enabled_state
73+
end function is_debug_enabled
74+
75+
subroutine log_debug_message(prefix, message)
76+
!! Emit debug message with prefix
77+
character(len=*), intent(in) :: prefix, message
78+
print '(a)', '['//trim(prefix)//'] '//trim(message)
79+
end subroutine log_debug_message
80+
81+
end module fortplot_debug_utils

src/external/fortplot_zlib_core.f90

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module fortplot_zlib_core
33
!! Ported from STB image libraries for self-contained PNG support
44
use, intrinsic :: iso_fortran_env, only: int8, int32
55
use iso_c_binding, only: c_ptr, c_loc, c_f_pointer, c_associated
6+
use fortplot_debug_utils, only: is_debug_enabled, log_debug_message
67
implicit none
78

89
private
@@ -42,8 +43,6 @@ module fortplot_zlib_core
4243
7, 7, 8, 8, 9, 9, 10, 10, &
4344
11, 11, 12, 12, 13, 13 ]
4445

45-
logical, save :: zlib_debug_initialized = .false.
46-
logical, save :: zlib_debug_enabled_state = .false.
4746

4847
! CRC32 lookup table (standard polynomial 0xEDB88320)
4948
integer(int32), parameter :: crc_table(0:255) = [ &
@@ -146,19 +145,19 @@ subroutine zlib_compress_into(input_data, input_len, output_data, output_len)
146145
logical :: debug_active
147146
character(len=160) :: debug_message
148147

149-
debug_active = is_zlib_debug_enabled()
148+
debug_active = is_debug_enabled('FORTPLOT_ZLIB_DEBUG')
150149
if (debug_active) then
151150
write(debug_message, '(a,i0)') 'zlib_compress_into begin, input_len=', &
152151
input_len
153-
call log_zlib_debug(debug_message)
152+
call log_debug_message('fortplot:zlib', debug_message)
154153
end if
155154

156155
call deflate_compress(input_data, input_len, compressed_block, compressed_block_len)
157156

158157
if (debug_active) then
159158
write(debug_message, '(a,i0)') 'deflate returned compressed_block_len=', &
160159
compressed_block_len
161-
call log_zlib_debug(debug_message)
160+
call log_debug_message('fortplot:zlib', debug_message)
162161
end if
163162

164163
output_len = 2 + compressed_block_len + 4
@@ -186,7 +185,7 @@ subroutine zlib_compress_into(input_data, input_len, output_data, output_len)
186185

187186
if (debug_active) then
188187
write(debug_message, '(a,i0)') 'zlib total output_len=', output_len
189-
call log_zlib_debug(debug_message)
188+
call log_debug_message('fortplot:zlib', debug_message)
190189
end if
191190
end subroutine zlib_compress_into
192191

@@ -983,72 +982,5 @@ function bit_reverse(value, num_bits) result(reversed_value)
983982
end do
984983
end function bit_reverse
985984

986-
pure function to_lower_char(ch) result(lower)
987-
!! Lower-case conversion for ASCII characters
988-
character(len=1), intent(in) :: ch
989-
character(len=1) :: lower
990-
integer :: code
991-
992-
lower = ch
993-
code = iachar(ch)
994-
if (code >= iachar('A') .and. code <= iachar('Z')) then
995-
lower = achar(code + 32)
996-
end if
997-
end function to_lower_char
998-
999-
logical function parse_debug_env(value)
1000-
!! Interpret an environment variable as boolean
1001-
character(len=*), intent(in) :: value
1002-
character(len=:), allocatable :: trimmed
1003-
integer :: i
1004-
1005-
trimmed = trim(adjustl(value))
1006-
1007-
if (len(trimmed) == 0) then
1008-
parse_debug_env = .true.
1009-
return
1010-
end if
1011-
1012-
do i = 1, len(trimmed)
1013-
trimmed(i:i) = to_lower_char(trimmed(i:i))
1014-
end do
1015-
1016-
select case (trimmed)
1017-
case ('0', 'false', 'off', 'no')
1018-
parse_debug_env = .false.
1019-
case default
1020-
parse_debug_env = .true.
1021-
end select
1022-
end function parse_debug_env
1023-
1024-
subroutine ensure_zlib_debug()
1025-
!! Lazy initialization for instrumentation flag
1026-
character(len=32) :: env_value
1027-
integer :: status
1028-
1029-
if (zlib_debug_initialized) return
1030-
1031-
call get_environment_variable('FORTPLOT_ZLIB_DEBUG', env_value, status=status)
1032-
if (status == 0) then
1033-
zlib_debug_enabled_state = parse_debug_env(env_value)
1034-
else
1035-
zlib_debug_enabled_state = .false.
1036-
end if
1037-
zlib_debug_initialized = .true.
1038-
end subroutine ensure_zlib_debug
1039-
1040-
logical function is_zlib_debug_enabled()
1041-
!! Query instrumentation flag
1042-
call ensure_zlib_debug()
1043-
is_zlib_debug_enabled = zlib_debug_enabled_state
1044-
end function is_zlib_debug_enabled
1045-
1046-
subroutine log_zlib_debug(message)
1047-
!! Emit debug message when instrumentation enabled
1048-
character(len=*), intent(in) :: message
1049-
if (is_zlib_debug_enabled()) then
1050-
print '(a)', '[fortplot:zlib] '//trim(message)
1051-
end if
1052-
end subroutine log_zlib_debug
1053985

1054986
end module fortplot_zlib_core

0 commit comments

Comments
 (0)