Lua 中如何实现类似C语言中 __FILE__, __LINE__, __FUNC__

本文介绍如何在Lua中实现类似C语言的__FILE__,__LINE__,__FUNC__功能,以便于调试代码。通过使用Lua的debug库,可以在运行时获取到当前执行的文件名、行号和函数名。

C语言中 __FILE__, __LINE__, __FUNC__ 分别可以表示当前代码行的 文件名,行号,函数名,这对于调试代码

非常方便,可以用printf打印出来



Lua本身并没有实现__FILE__, __LINE__, __FUNC__,当我们可以这样加上


首先在需要的文件中引入函数定义

function __FILE__() return debug.getinfo(2,'S').source end
function __LINE__() return debug.getinfo(2, 'l').currentline end


然后在要打印的地方

print("Line at "..__LINE__()..", FILE at "..__FILE__()..", in func: "..(debug.getinfo(1,"n").name or "NOT in a func call"))

好了,这样就可以看到输出的行号,文件名 和函数名(如果有的话)

--函数名称:my_getedataLen_Type --功能说明:获取对应数据的类型和长度 --形 参:data:待传入的数据 --返 回 值:datalen:数据的长度 data_type:数据的类型 function my_getdataLen_Type(data) local datalen = -1 local data_type = type(data) if data_type == 'number' then -- 将数字转换为字符串后再计算长度 datalen = string.len(tostring(data)) elseif data_type == 'string' then datalen = string.len(data) elseif data_type == 'table' then datalen = #data end return datalen, data_type end --函数名称:my_split --功能说明:将一个字符串按照指定的分隔符分割成多个子字符串,并将这些子字符串存储在一个表(数组)中返回。 --形 参:str:字符串 pat:分隔符 --返 回 值:返回存储所有子字符串的表 t function my_split(str, pat) local i = 1 -- 初始化计数器 i,用于记录分割后的子字符串数量 local t = {} -- 创建一个空表 t,用于存储分割后的子字符串 local last_end = 0 -- 初始化变量 last_end,记录上一个分隔符结束的位置 local s, e = string.find(str, pat, 1) -- 在字符串 str 中从位置 1 开始查找分隔符 pat,返回分隔符的起始位置 s 和结束位置 e while s -- 如果找到了分隔符(s 不为 nil) do table.insert(t, string.sub(str, last_end + 1, last_end + s - last_end - 1)) -- 截取从 last_end + 1 到分隔符起始位置 s - 1 ?淖幼址⒔洳迦氲奖?t 中 last_end = e -- 更新 last_end 为当前分隔符结束的位置 e s, e = string.find(str, pat, last_end + 1) -- 在字符串 str 中从 last_end + 1 的位置开始继续查找分隔符 pat i = i + 1 -- 增加计数器 i end if last_end <= #str --如果 last_end 小于等于字符串 str 的长度(即还有未处理的剩余部分) then cap = string.sub(str, last_end + 1) -- 截取从 last_end + 1 到字符串结尾的子字符串 table.insert(t, cap) -- 将该子字符串插入到表 t 中 end return t -- 返回存储所有子字符串的表 t end --函数名称:my_ShowRecord_FromFile --功能说明:用于处理从文件中读取的字符串数据,按行分割后,去除每行中的回车符和换行符,并在每行末尾添加分号,最后将处理后的数据添加到记录显示区域。 --形 参:mystr:读取的字符串 --返 回 值:无 function my_ShowRecord_FromFile(mystr) local line_data = my_split(mystr, '% ') -- -- 调用 my_split 函数,将 mystr 字符串按照换行符 '\n' 分割成多个子字符串,存储在 line_data 表中 for i = 1, #(line_data) -- 遍历 line_data 表中的每个元素 do if line_data[i] ~= nil -- 如果当前元素不为空 then feed_dog() -- 调用 feed_dog 函数(可能是用于防止系统“看门狗”超时等操作) -- 去除当前行数据中的回车符 ' ' 和换行符 '\n' line_data[i] = string.gsub(line_data[i], "% ", '') line_data[i] = string.gsub(line_data[i], "%\n", '') --line_data[i] = line_data[i]..';' -- 在当前行数据末尾添加分号 ';' -- 将处理后的行数据添加到记录显示区域 sc_ShowRecord 中,格式为 '$NUM;' 加上处理后的行数据 set_history_graph_value(26,6,line_data[i]) set_value(26,9,line_data[i]) end end end function my_read_filedata(file) local count = 0 --每次要读取到的字节数 local read_cnt = 0 --要读取多少次 多少块 local offset = 0 --读取偏移地址 local all_byte = 0 --要读取的所有的字节数 local read_byte_Tb = {} --用于存储从文件中读取的原始字节数据 local read_char_Tb = {} --用于将字节数据转换为字符数据 local read_str = '' --用于存储最终拼接成的字符串形式的文件内容。 local open_state = file_open(file, FA_READ) --以只读的方式进行打开 if open_state == true then set_text(26, 5, 'open on') --获取当前文件大小 all_byte = file_size() if all_byte > 0 then read_cnt = math.modf(all_byte/WriteOnceSize) --计算出完整的有多少块 if all_byte % WriteOnceSize > 0 then read_cnt = read_cnt + 1 --最后一块未满的一块 end for i = 1, read_cnt do --复位读字节数组 read_byte_Tb = {} --用于存储从文件中读取的原始字节数据 read_char_Tb = {} --用于将字节数据转换为字符数据 --计算读取的偏移位置 offset = (i - 1) * WriteOnceSize local offst_result = file_seek(offset) --文件偏移失败 if offst_result == false then set_text(26, 5, 'offest fail') break else set_text(26, 5, 'offest on') end --计算本次读的个数 count = WriteOnceSize if i == read_cnt --最后一块 then if all_byte % WriteOnceSize > 0 then count = all_byte % WriteOnceSize end end --读取字节转换为字符并拼接成字符串 read_byte_Tb = file_read(count) if #(read_byte_Tb) > 0 then set_text(26, 5, 'read ok') for j = 0, #(read_byte_Tb) do read_char_Tb[j + 1] = string.char(read_byte_Tb[j]) --将当前字节的数据转换为字符 从下标1开始 Lua 中数组索引通常从 1 开始 end read_str = read_str..table.concat(read_char_Tb) --将read_char_Tb数组中的每一个数据拼接到一个字符串中 追加到 read_str 字符串变量中,从而逐步构建完整的文件内容字符串 elseif read_byte_Tb ==nil --显示文件读取错误的提示信息,提醒用户重新尝试 then set_text(26, 5, 'read fail') break; end end my_ShowRecord_FromFile(read_str) else --区域显示文件不存在或无内容的提示信息,提醒用户检查 SD 卡中的文件 set_text(26, 5, 'dont exist') end else set_text(26, 5, 'open fail') end --关闭文件 file_close() end --函数名称:my_write_filedata --功能说明:使对应的要写入的数据的类型值 转成数值类型存到发送函数中 利用file_write() :函数进行发送 --形 参:file:文件的绝对路径 data:要写入SD卡的数据 open_mode:打开的模式 --返 回 值:无 function my_write_filedata(file, data, open_mode) local count = 0 -- 当前写入块大小(每次) local write_cnt = 0 -- 总写入次数 要写入几次才能写完 local seek_ops = 0 -- 写入偏移量 local all_byte = 0 -- 原文件大小(追加模式用) 在原有文件大小的后面进行添加 --获取待写入数据的数据类型和长度 local wrire_len, data_type = my_getdataLen_Type(data) local write_byte_Tb = {} -- 写入缓存 --打开文件 local open_state = file_open(file, open_mode) if open_state == true then set_text(28, 6, 'open on') --获取当前文件大小,仅在追加文件末尾写入执行 追加模式获取原文件大小 if open_mode == add_write then all_byte = file_size() end --若有数据要写入 if wrire_len > 0 then --计算data要写多少次 计算写入次数 也就是有多少块数据 write_cnt = math.modf(wrire_len / WriteOnceSize) if wrire_len % WriteOnceSize > 0 then write_cnt = write_cnt + 1 end --循环分块写入 for i = 1, write_cnt do --复位写字节数组 清空临时表 write_byte_Tb = {} --计算写的位置 seek_ops = (i - 1) * WriteOnceSize +all_byte local offst_result = file_seek(seek_ops) --若文件偏移失败 if offst_result == false then --提示信息 set_text(28, 6, 'offst off') break else set_text(28, 6, 'offst ok') end --计算本次写的个数(字节数) count = WriteOnceSize --当前写入块的大小 if i == write_cnt --最后一块 then if wrire_len % WriteOnceSize > 0 then count = wrire_len % WriteOnceSize --当前写入块的大小 end end --填充写入字节数组 准备写入数据 for j = 1, count do --if data_type == 'string' or data_type == 'number' if data_type == 'string' then --字符串类型,将每个字符转换为字节数组 -- 字符串转字节 --local str = (data_type == 'string') and data or tostring(data) --write_byte_Tb[j - 1] = string.byte(str, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j)) --tonumber():将结果显式转换为数值类型 --字符串类型,将每个字符转换为字节数组 填写到发送函数中 write_byte_Tb[j - 1] = tonumber(string.byte(data, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j))) elseif data_type == 'table' then --数组类型,字节赋值 -- 直接使用字节数组 write_byte_Tb[j - 1] = data[((i - 1) * WriteOnceSize + j)] end end -- 执行写入 local IswriteOK = file_write(write_byte_Tb) if IswriteOK == false then set_text(28, 6, 'write off') else set_text(28, 6, 'write ok') end end end else set_text(28, 6, 'open fail') end --关闭文件 file_close() end local gas =0 --保存采集到气体的浓度值 --定时回调函数,系统每隔1秒钟自动调用 function on_systick() gas= get_value(0,14) --local tabl ={} --tabl[1]=a --local str = " ".. tostring(gas) local str = tostring(gas).." " my_write_filedata(sd_dir..'/'..'1.txt', str, add_write) --读取 my_read_filedata(sd_dir..'/'..'1.txt') end 现在读写都能够正常运行 但发现了一个bug 现在sd已经有下入的浮点数数据 我想要利用函数my_read_filedata进行只读 结果是卡在一个数进行不动了 这是什么原因
最新发布
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值