{"id":5719,"date":"2026-05-18T18:56:09","date_gmt":"2026-05-18T09:56:09","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5719"},"modified":"2026-05-18T18:59:12","modified_gmt":"2026-05-18T09:59:12","slug":"basic-make_unique-log-to-file","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/18\/basic-make_unique-log-to-file\/","title":{"rendered":"[Basic]make_unique+\ud30c\uc77c\uc5d0 \ub85c\uadf8\uae30\ub85d\/log to file"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798 \ucf54\ub4dc\ub294 mini_mediasever_1\uc758 \uc77c\ubd80\ub85c\uc9c1\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>The code below uses some of the logic from mini_mediasever_1.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffbmake_unique\ub97c \uc0ac\uc6a9\ud574\uc11c \ud30c\uc77c\uc5d0 \ub85c\uadf8\ub97c \uae30\ub85d\ud558\ub294 \ucf54\ub4dc\uc785\ub2c8\ub2e4.<br>This is code that logs to a file using make_unique.<\/p>\n\n\n\n<p>\u2714\ufe0fmake_unique \ub294new + unique_ptr \uc0dd\uc131\uc790\ub97c \ud569\uce5c \uac83.<br>make_unique is a combination of the new + unique_ptr constructors.<\/p>\n\n\n\n<p>\u2714\ufe0f unique_ptr : <br>\ud55c \uacf3\ub9cc \ud560\ub2f9\uac00\ub2a5\ud55c \ud3ec\uc778\ud130,\uc18c\uc720\uad8c \uc774\ub3d9\ub9cc\uac00\ub2a5<br>A pointer that can be assigned to only one location, ownership can only be transferred.<\/p>\n\n\n\n<p>\u2714\ufe0f make_unique \ub294 \uc2a4\ub9c8\ud2b8 \ud3ec\uc778\ud130\ub85c \uc790\ub3d9 \uba54\ubaa8\ub9ac \ud574\uc81c<br>make_unique automatically frees memory using smart pointers.<\/p>\n\n\n\n<p>\u2714\ufe0f make_unique\ub97c \uc0ac\uc6a9\ud558\uba74 Logger\ud074\ub798\uc2a4\uac00 \uc18c\uba78\ub420 \ub54c \ud30c\uc77c\ub3c4 \uc790\ub3d9\uc73c\ub85c \ub2eb\ud799\ub2c8\ub2e4.<br>If you use make_unique, the file is automatically closed when the Logger class is destroyed.<\/p>\n\n\n\n<p>\u2714\ufe0f \ubc29\uc1a1\uc911 \ubc29\uc1a1\uc774 \ub04a\uae34\ud6c4 \ud30c\uc77c\ub3c4 \ub2eb\uac8c \ud574\uc11c \ud50c\ub808\uc774\uc5b4\uc5d0\uc11c \uc5f4\ub9ac\uc9c0 \uc54a\ub294 \ubb38\uc81c\ub97c \ubc29\uc9c0\ud558\uae30 \uc704\ud568\uc785\ub2c8\ub2e4.<br>This is to prevent the file from opening in the player by closing it after the broadcast is cut off.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;memory&gt;\n#include &lt;fstream&gt;\n#include &lt;string&gt;\n\nclass Logger {\n    std::ofstream file_; \/\/private\npublic:\n    Logger(const std::string&amp; filename) {\n        file_.open(filename);\n        file_ &lt;&lt; \"=== \ub85c\uadf8 \uc2dc\uc791 \/ Log Start ===\\n\";\n        std::cout &lt;&lt; filename &lt;&lt; \" \ud30c\uc77c \uc5f4\ub9bc \/ File Open\\n\";\n    }\n\n    void log(const std::string&amp; msg) {\n        file_ &lt;&lt; msg &lt;&lt; \"\\n\";\n        std::cout &lt;&lt; \"\ub85c\uadf8 \uae30\ub85d \/ log record: \" &lt;&lt; msg &lt;&lt; \"\\n\";\n    }\n\n    ~Logger() {\n        file_ &lt;&lt; \"=== \ub85c\uadf8 \uc885\ub8cc \/ log exit ===\\n\";\n        file_.close();\n        std::cout &lt;&lt; \"\ud30c\uc77c \ub2eb\ud798 \/ file close\\n\";\n    }\n};\n\nint main() {\n    {\n        \/\/ \uc5ec\uae30\uc11c \ud30c\uc77c \uc5f4\ub9bc \/  Open file here\n        auto logger = std::make_unique&lt;Logger&gt;(\"app.log\");\n        logger-&gt;log(\"\ud504\ub85c\uadf8\ub7a8 \uc2dc\uc791 \/ Start program\");\n        logger-&gt;log(\"\uc791\uc5c5 \uc911...\/ Working...\");\n    } \/\/ \uc5ec\uae30 \ub098\uac00\uba74 \uc790\ub3d9\uc73c\ub85c \ud30c\uc77c \ub2eb\ud798 \/ The file closes automatically when you leave here\n\n    std::cout &lt;&lt; \"main \ub05d\/end\\n\";\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ main.cpp -o main<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/main<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uacb0\uacfc \/ result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MacBookAir make_unique % ls\nmain.cpp\n\nMacBookAir make_unique % g++ main.cpp -o main\n\nMacBookAir make_unique % ls\nmain\t\tmain.cpp\n\nMacBookAir make_unique % .\/main\napp.log \ud30c\uc77c \uc5f4\ub9bc \/ File Open\n\ub85c\uadf8 \uae30\ub85d \/ log record: \ud504\ub85c\uadf8\ub7a8 \uc2dc\uc791 \/ Start program\n\ub85c\uadf8 \uae30\ub85d \/ log record: \uc791\uc5c5 \uc911...\/ Working...\n\ud30c\uc77c \ub2eb\ud798 \/ file close\nmain \ub05d\/end\n\nMacBookAir make_unique % ls\napp.log\t\tmain\t\tmain.cpp\n\nMacBookAir make_unique % cat app.log\n=== \ub85c\uadf8 \uc2dc\uc791 \/ Log Start ===\n\ud504\ub85c\uadf8\ub7a8 \uc2dc\uc791 \/ Start program\n\uc791\uc5c5 \uc911...\/ Working...\n=== \ub85c\uadf8 \uc885\ub8cc \/ log exit ===\n\nMacBookAir make_unique % <\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc544\ub798 \ucf54\ub4dc\ub294 mini_mediasever_1\uc758 \uc77c\ubd80\ub85c\uc9c1\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.The code below uses some of the logic from mini_mediasever_1. \ud83d\udc49\ud83c\udffbmake_unique\ub97c \uc0ac\uc6a9\ud574\uc11c \ud30c\uc77c\uc5d0 \ub85c\uadf8\ub97c \uae30\ub85d\ud558\ub294 \ucf54\ub4dc\uc785\ub2c8\ub2e4.This is code that logs to a file using make_unique. \u2714\ufe0fmake_unique \ub294new + unique_ptr \uc0dd\uc131\uc790\ub97c \ud569\uce5c \uac83.make_unique is a combination of the new + unique_ptr constructors. \u2714\ufe0f unique_ptr : \ud55c \uacf3\ub9cc \ud560\ub2f9\uac00\ub2a5\ud55c \ud3ec\uc778\ud130,\uc18c\uc720\uad8c \uc774\ub3d9\ub9cc\uac00\ub2a5A pointer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5719","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5719"}],"version-history":[{"count":1,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5719\/revisions"}],"predecessor-version":[{"id":5720,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5719\/revisions\/5720"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}