Skip to content

Instantly share code, notes, and snippets.

@antimon2
Created October 7, 2020 23:34
Show Gist options
  • Save antimon2/025c3ab9a4c92c52bd9baa1125bd4831 to your computer and use it in GitHub Desktop.
Save antimon2/025c3ab9a4c92c52bd9baa1125bd4831 to your computer and use it in GitHub Desktop.
at_withlog.jl.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.497Z",
"end_time": "2020-10-08T08:31:49.376000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "versioninfo()",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Julia Version 1.5.2\nCommit 539f3ce943 (2020-09-23 23:17 UTC)\nPlatform Info:\n OS: Linux (x86_64-pc-linux-gnu)\n CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-9.0.1 (ORCJIT, skylake)\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### simple implementation"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.500Z",
"end_time": "2020-10-08T08:31:49.853000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function start_log(func_name)\n println(\"$(func_name): write start log\")\nend\n\nfunction end_log(func_name)\n println(\"$(func_name): write end log\")\nend",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "end_log (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.501Z",
"end_time": "2020-10-08T08:31:49.927000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "macro withlog_simple(ex)\n if Meta.isexpr(ex, :function)\n s = string(ex.args[1].args[1])\n quote\n function $(esc(ex.args[1].args[1]))($(esc.(ex.args[1].args[2:end])...))\n start_log($s)\n $(esc(ex.args[2]))\n end_log($s)\n end\n end\n else\n esc(ex)\n end\nend",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "@withlog_simple (macro with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.503Z",
"end_time": "2020-10-08T08:31:50.259000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@macroexpand @withlog_simple function add_simple(a, b)\n println(a + b)\nend",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "quote\n #= In[3]:5 =#\n function add_simple(a, b)\n #= In[3]:5 =#\n #= In[3]:6 =#\n Main.start_log(\"add_simple\")\n #= In[3]:7 =#\n begin\n #= In[4]:1 =#\n #= In[4]:2 =#\n println(a + b)\n end\n #= In[3]:8 =#\n Main.end_log(\"add_simple\")\n end\nend"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.504Z",
"end_time": "2020-10-08T08:31:50.318000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@withlog_simple function add_simple(a, b)\n println(a + b)\nend",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "add_simple (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.506Z",
"end_time": "2020-10-08T08:31:50.341000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "add_simple(1, 2)",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "add_simple: write start log\n3\nadd_simple: write end log\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### with `MacroTools.jl`"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.509Z",
"end_time": "2020-10-08T08:32:00.139000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "]add MacroTools",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.510Z",
"end_time": "2020-10-08T08:32:00.214000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "using MacroTools",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.511Z",
"end_time": "2020-10-08T08:32:00.289000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "macro withlog(ex)\n fndef = splitdef(ex)\n s = string(fndef[:name])\n quote\n function $(esc(fndef[:name]))($(esc.(fndef[:args])...))\n start_log($s)\n $(esc(fndef[:body]))\n end_log($s)\n end\n end\n # fndef[:name] = esc(fndef[:name])\n # fndef[:args] = esc.(fndef[:args])\n # bdy = quote\n # start_log($s)\n # $(esc(fndef[:body]))\n # end_log($s)\n # end\n # fndef[:body] = bdy\n # MacroTools.combinedef(fndef)\nend",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "@withlog (macro with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.514Z",
"end_time": "2020-10-08T08:32:00.866000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@macroexpand @withlog function add(a, b)\n println(a + b)\nend",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "quote\n #= In[9]:5 =#\n function add(a, b)\n #= In[9]:5 =#\n #= In[9]:6 =#\n Main.start_log(\"add\")\n #= In[9]:7 =#\n begin\n #= In[10]:1 =#\n #= In[10]:2 =#\n println(a + b)\n end\n #= In[9]:8 =#\n Main.end_log(\"add\")\n end\nend"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.516Z",
"end_time": "2020-10-08T08:32:00.927000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@withlog function add(a, b)\n println(a + b)\nend",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "add (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-10-07T23:31:49.517Z",
"end_time": "2020-10-08T08:32:00.930000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "add(1, 2)",
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": "add: write start log\n3\nadd: write end log\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.5",
"display_name": "Julia 1.5.2",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.5.2"
},
"gist": {
"id": "",
"data": {
"description": "at_withlog.jl.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment